Skip to content

Instantly share code, notes, and snippets.

@victorBaro
Created March 22, 2015 22:44
Show Gist options
  • Save victorBaro/cd0010b934c17587615c to your computer and use it in GitHub Desktop.
Save victorBaro/cd0010b934c17587615c to your computer and use it in GitHub Desktop.
Swift test from original Nick Lockwood article: https://gist.github.com/nicklockwood/d374033b27c62662ac8d (animated UILabel on text changes)
import UIKit
class ViewController: UIViewController {
let magicLabel = AnimatedLabel(frame: CGRectMake(100, 100, 300, 20));
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
magicLabel.text = "Magic label"
self.view.addSubview(magicLabel);
}
@IBAction func buttonPressed(sender: UIButton) {
if let labelText = magicLabel.text? {
magicLabel.text = newString(labelText)
}
}
func newString(previousString: String) -> String {
if previousString == "Magic label" {
return "Aumagically animated"
}
return "Magic label"
}
}
class AnimatedLabel: UILabel {
override func actionForLayer(layer: CALayer!, forKey event: String!) -> CAAction! {
if (event == "contents"){
var transition = CATransition()
transition.type = kCATransitionMoveIn
transition.subtype = kCATransitionFromTop
return transition
}
return super.actionForLayer(layer, forKey: event)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment