In this example we are creating 2 labels.
Both are going to be animated, one of them twice.
Please carefully examine the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class ViewController: UIViewController { // first creating 2 UILabels var meme1: UILabel! var meme2: UILabel! override func viewDidLoad() { super.viewDidLoad() func flyMemes () { // Creating our function meme1.text = "Brace yourself!" meme1.font = UIFont.systemFontOfSize(25) meme1.textColor = UIColor.whiteColor() // This is all just styling the Label meme1.sizeToFit() meme1.center = CGPoint(x: 200, y: -50) // Starting position of the label view.addSubview(meme1) // Important! Adding the Label to the Subview, so we can actually see it. //Animation options. Play around with it. UIView.animateWithDuration(0.9, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.0, options: .CurveLinear, animations: { self.meme1.center = CGPoint(x: 200, y:50 ) // Ending position of the Label }, completion: nil) UIView.animateWithDuration(0.6, delay: 1.1, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.0, options: .CurveLinear, animations: { self.meme1.center = CGPoint(x: 200, y:150+90 ) }, completion: nil) |
Then we call the flying Labels wherever we need it:
1 2 |
//call it flyMemes() |
Adding meme1 = UILabel() to the begging of CREATING LABEL
Do you need help ? Not sure if that was a question ? 🙂