Animate UILabel using CATransition
UIView.animate(withDuration: 0.5) { | |
//animate your UILable | |
} |
Why to use CATransition
- kCATransitionFade
- kCATransitionMoveIn
- kCATransitionPush
- kCATransitionReveal
subtype: This property tells what type of direction we want for our animation, its an optional property. These are also 4 types
- kCATransitionFromRight
- kCATransitionFromLeft
- kCATransitionFromTop
- kCATransitionFromBottom
How to use CATransition animation on UILabel
2: Open “ViewController.swift” , and create IBOutlet, IBAction’s for the UILabel and UIButton’s respectively as shown here
// | |
// ViewController.swift | |
// CATransition-Example | |
// | |
// Created by Aman Aggarwal on 29/08/18. | |
// Copyright © 2018 iostutorialjunction.com. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var lblCounter: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func incrementCounter(_ sender: Any) { | |
} | |
@IBAction func decrementCounter(_ sender: Any) { | |
} | |
} | |
3: Open “Main.storyboard” , and connect the IBOutlet and IBAction’s. Connect incrementCounter to “+” UIButton and decrementCounter to “-” UIButton.
4: Open “ViewController.swift” and add the below animation code as an extension.
// | |
// ViewController.swift | |
// CATransition-Example | |
// | |
// Created by Aman Aggarwal on 29/08/18. | |
// Copyright © 2018 iostutorialjunction.com. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var lblCounter: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func incrementCounter(_ sender: Any) { | |
} | |
@IBAction func decrementCounter(_ sender: Any) { | |
} | |
} | |
extension CALayer { | |
func bottomAnimation(duration:CFTimeInterval) { | |
let animation = CATransition() | |
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) | |
animation.duration = duration | |
animation.type = kCATransitionPush | |
animation.subtype = kCATransitionFromTop | |
self.add(animation, forKey: kCATransitionPush) | |
} | |
func topAnimation(duration:CFTimeInterval) { | |
let animation = CATransition() | |
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) | |
animation.duration = duration | |
animation.type = kCATransitionPush | |
animation.subtype = kCATransitionFromBottom | |
self.add(animation, forKey: kCATransitionPush) | |
} | |
} |
If you look at the animation code we have two functions,
i) bottomAnimation
ii) topAnimation
Both the codes had almost same code and the only difference is the “subType” specified for the animation direction. “bottomAnimation” function had direction for animation “from Top” as specified in “subType“property of the transition and “topAnimation” function had direction for animation “from Bottom” as specified in “subType“property of the transition.
Using the bottomAnimation and topAnimation
// | |
// ViewController.swift | |
// CATransition-Example | |
// | |
// Created by Aman Aggarwal on 29/08/18. | |
// Copyright © 2018 iostutorialjunction.com. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var lblCounter: UILabel! | |
var counter = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func incrementCounter(_ sender: Any) { | |
lblCounter.layer.bottomAnimation(duration: 0.7) | |
counter = counter + 1 | |
lblCounter.text = "\(counter)" | |
} | |
@IBAction func decrementCounter(_ sender: Any) { | |
if counter > 0 { | |
lblCounter.layer.topAnimation(duration: 0.7) | |
counter = counter - 1 | |
lblCounter.text = "\(counter)" | |
} | |
} | |
} | |
extension CALayer { | |
func bottomAnimation(duration:CFTimeInterval) { | |
let animation = CATransition() | |
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) | |
animation.duration = duration | |
animation.type = kCATransitionPush | |
animation.subtype = kCATransitionFromTop | |
self.add(animation, forKey: kCATransitionPush) | |
} | |
func topAnimation(duration:CFTimeInterval) { | |
let animation = CATransition() | |
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) | |
animation.duration = duration | |
animation.type = kCATransitionPush | |
animation.subtype = kCATransitionFromBottom | |
self.add(animation, forKey: kCATransitionPush) | |
} | |
} |