Created
March 22, 2015 22:44
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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