Skip to content

Instantly share code, notes, and snippets.

@leoiphonedev
Created September 15, 2018 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoiphonedev/49b955c214e764b22a98dcfcf5ecc01e to your computer and use it in GitHub Desktop.
Save leoiphonedev/49b955c214e764b22a98dcfcf5ecc01e to your computer and use it in GitHub Desktop.
Compete code for demonstarting how to use fade animation on UILabel using CATransition Animation in swift4
//
// 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment