Translate To Preferred Language

Search ObiokusThoughts (Posts Only)

Please Read Today's Featured Post

The Inherent Rationale Of Perception

Perception is one of the most important factors affecting our day to day lives as human beings.  I believe it is one of the most underrate...

Donate

Template for Simple Label Animation in Swift (some bugs may occur)



//

//  ViewAnimationController.swift
//  Tutorial
//
//  Created by Obioku Bassey Obotette on 2/13/20.
//  Copyright © 2020 Obioku Bassey Obotette. All rights reserved.
//

import UIKit

class ViewAnimationController: UIViewController, UICollisionBehaviorDelegate, UIDynamicAnimatorDelegate {
    //View Frame is 414 width x 896 height at 0 x 0
    //Text View frame in x: 69 y: 127 width: 240 height: 128
    @IBOutlet weak var resetButton: UIButton!
    @IBOutlet weak var animateButton: UIButton!
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var label0: UILabel!
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    @IBOutlet weak var label4: UILabel!
    @IBOutlet weak var label5: UILabel!
    @IBOutlet weak var label6: UILabel!
    @IBOutlet weak var label7: UILabel!
    var touch: UITapGestureRecognizer!
    var hold: UILongPressGestureRecognizer!
    var collision: UICollisionBehavior!
    var dynamicA: UIDynamicAnimator!
    var dynamicB: UIDynamicBehavior!
    var dynamicI: UIDynamicItemBehavior!
    var resetFrames: [CGRect]!
    var resetText: [String]!
    
    
    var random: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        resetFrames = [label0!.frame,label1!.frame,label2!.frame,label3!.frame,label4!.frame,label5!.frame,label6!.frame,label7!.frame,textView!.frame]
        resetText = [label0!.text,label1!.text,label2!.text,label3!.text,label4!.text,label5!.text,label6!.text,label7!.text,textView!.text] as! [String]
        collision = UICollisionBehavior.init(items: [textView!, label0!,label1!,label2!,label3!,label4!,label5!,label6!,label7!])
        dynamicI = UIDynamicItemBehavior.init(items: [textView!, label0!,label1!,label2!,label3!,label4!,label5!,label6!,label7!])
        collision.collisionDelegate = self //as! UICollisionBehaviorDelegate
        collision.translatesReferenceBoundsIntoBoundary = true
        collision.collisionMode = .everything
        dynamicI.elasticity = 0.0
        dynamicI.allowsRotation = true
        dynamicI.angularResistance = 10.0
        dynamicI.charge = 0.0
        dynamicI.friction = 0.0
        dynamicA = UIDynamicAnimator.init(referenceView: self.view)
        dynamicA.delegate = self //as! UIDynamicAnimatorDelegate
        dynamicA.addBehavior(collision!)
        let touch0 = UITapGestureRecognizer.init(target: self, action: #selector(self.touched0))
        let touch1 = UITapGestureRecognizer.init(target: self, action: #selector(self.touched1))
        let touch2 = UITapGestureRecognizer.init(target: self, action: #selector(self.touched2))
        let touch3 = UITapGestureRecognizer.init(target: self, action: #selector(self.touched3))
        let touch4 = UITapGestureRecognizer.init(target: self, action: #selector(self.touched4))
        let touch5 = UITapGestureRecognizer.init(target: self, action: #selector(self.touched5))
        let touch6 = UITapGestureRecognizer.init(target: self, action: #selector(self.touched6))
        let touch7 = UITapGestureRecognizer.init(target: self, action: #selector(self.touched7))
        let pan0 = UIPanGestureRecognizer.init(target: self, action: #selector(self.panned0))
        let pan1 = UIPanGestureRecognizer.init(target: self, action: #selector(self.panned1))
        let pan2 = UIPanGestureRecognizer.init(target: self, action: #selector(self.panned2))
        let pan3 = UIPanGestureRecognizer.init(target: self, action: #selector(self.panned3))
        let pan4 = UIPanGestureRecognizer.init(target: self, action: #selector(self.panned4))
        let pan5 = UIPanGestureRecognizer.init(target: self, action: #selector(self.panned5))
        let pan6 = UIPanGestureRecognizer.init(target: self, action: #selector(self.panned6))
        let pan7 = UIPanGestureRecognizer.init(target: self, action: #selector(self.panned7))
        let viewTouch = UITapGestureRecognizer(target: self, action: #selector(self.animateTextView(_:)))
        //self.touch.numberOfTapsRequired = 1
        self.view.isUserInteractionEnabled = true
        self.view.isMultipleTouchEnabled = true
        self.view.addGestureRecognizer(viewTouch)
        //self.hold = UILongPressGestureRecognizer(target: self, action: #selector(self.pressed))
        //self.hold.minimumPressDuration = 0.7
        //self.touch.require(toFail: self.hold)
        //self.hold.require(toFail: self.touch)
        self.label0.isUserInteractionEnabled = true
        self.label0.isMultipleTouchEnabled = true
        self.label0.addGestureRecognizer(touch0)
        self.label0.addGestureRecognizer(pan0)
        touch0.require(toFail: pan0)
        pan0.require(toFail: touch0)
        self.label1.isUserInteractionEnabled = true
        self.label1.isMultipleTouchEnabled = true
        self.label1.addGestureRecognizer(touch1)
        self.label1.addGestureRecognizer(pan1)
        touch1.require(toFail: pan1)
        pan1.require(toFail: touch1)
        self.label2.isUserInteractionEnabled = true
        self.label2.isMultipleTouchEnabled = true
        self.label2.addGestureRecognizer(touch2)
        self.label2.addGestureRecognizer(pan2)
        touch2.require(toFail: pan2)
        pan2.require(toFail: touch2)
        self.label3.isUserInteractionEnabled = true
        self.label3.isMultipleTouchEnabled = true
        self.label3.addGestureRecognizer(touch3)
        self.label3.addGestureRecognizer(pan3)
        touch3.require(toFail: pan3)
        pan3.require(toFail: touch3)
        self.label4.isUserInteractionEnabled = true
        self.label4.isMultipleTouchEnabled = true
        self.label4.addGestureRecognizer(touch4)
        self.label4.addGestureRecognizer(pan4)
        touch4.require(toFail: pan4)
        pan4.require(toFail: touch4)
        self.label5.isUserInteractionEnabled = true
        self.label5.isMultipleTouchEnabled = true
        self.label5.addGestureRecognizer(touch5)
        self.label5.addGestureRecognizer(pan5)
        touch5.require(toFail: pan5)
        pan5.require(toFail: touch5)
        self.label6.isUserInteractionEnabled = true
        self.label6.isMultipleTouchEnabled = true
        self.label6.addGestureRecognizer(touch6)
        self.label6.addGestureRecognizer(pan6)
        touch6.require(toFail: pan6)
        pan6.require(toFail: touch6)
        self.label7.isUserInteractionEnabled = true
        self.label7.isMultipleTouchEnabled = true
        self.label7.addGestureRecognizer(touch7)
        self.label7.addGestureRecognizer(pan7)
        touch7.require(toFail: pan7)
        pan7.require(toFail: touch7)
        
        //self.animateButton.setTitleColor(UIColor.init(red: CGFloat.random(in: 0...255), green: CGFloat.random(in: 0...255), blue: CGFloat.random(in: 0...255), alpha: 1), for: .selected)
        // Do any additional setup after loading the view.
    }
    
    
    @objc func touched0(_ tap: UITapGestureRecognizer){
        //let sender = tap.view as! UILabel
        //print(sender.text!)
        switch Int.random(in: 0...2)
        {
        case 0:
            UIView.animate(withDuration: 1.0) {
            self.label0.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
        }
        UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
            self.label0.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        }) { (true) in
            print("size")
            }
        case 1:
            UIView.animate(withDuration: 1.0) {
            self.label0.transform = CGAffineTransform.init(rotationAngle: .pi)
            }
        UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
            self.label0.transform = CGAffineTransform(rotationAngle: 2 * .pi)
        }) { (true) in
            print("spin")
            }
        case 2:
            if label0.frame.minX < self.view.center.x {
                UIView.animate(withDuration: 1.0) {
                    self.label0.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label0.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }) { (true) in
                    print("slide >")
                }
            }else{
                UIView.animate(withDuration: 1.0) {
                    self.label0.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label0.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }) { (true) in
                    print("slide <")
                }
            }
            
        default: print("error")
        }
        
        
    }
    @objc func touched1(_ tap: UITapGestureRecognizer){
        //let sender = tap.view as! UILabel
        //print(sender.text!)
        switch Int.random(in: 0...2)
        {
        case 0:
            UIView.animate(withDuration: 1.0) {
                self.label1.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label1.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }) { (true) in
               print("size")
            }
        case 1:
            UIView.animate(withDuration: 1.0) {
                self.label1.transform = CGAffineTransform.init(rotationAngle: .pi)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label1.transform = CGAffineTransform(rotationAngle: 2 * .pi)
            }) { (true) in
               print("spin")
            }
        case 2:
            if label1.frame.minX < self.view.center.x {
                UIView.animate(withDuration: 1.0) {
                    self.label1.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label1.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }) { (true) in
                    print("slide >")
                }
            }else{
                UIView.animate(withDuration: 1.0) {
                    self.label1.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label1.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }) { (true) in
                    print("slide <")
                }
            }
            
            
        default: print("error")
        }
        
    }
    @objc func touched2(_ tap: UITapGestureRecognizer){
        //let sender = tap.view as! UILabel
        //print(sender.text!)
        switch Int.random(in: 0...2)
        {
        case 0:
            UIView.animate(withDuration: 1.0) {
                self.label2.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label2.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }) { (true) in
             print("size")
            }
        case 1:
            UIView.animate(withDuration: 1.0) {
                self.label2.transform = CGAffineTransform.init(rotationAngle: .pi)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label2.transform = CGAffineTransform(rotationAngle: 2 * .pi)
            }) { (true) in
              print("spin")
            }
        case 2:
            if label2.frame.minX < self.view.center.x {
                UIView.animate(withDuration: 1.0) {
                    self.label2.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label2.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }) { (true) in
                    print("slide >")
                }
            }else{
                UIView.animate(withDuration: 1.0) {
                    self.label2.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label2.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }) { (true) in
                    print("slide <")
                }
            }
            
            
         default: print("error")
        }
        
    }
    @objc func touched3(_ tap: UITapGestureRecognizer){
        //let sender = tap.view as! UILabel
        //print(sender.text!)
        switch Int.random(in: 0...2)
        {
        case 0:
            UIView.animate(withDuration: 1.0) {
                self.label3.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label3.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }) { (true) in
              print("size")
            }
        case 1:
            UIView.animate(withDuration: 1.0) {
                self.label3.transform = CGAffineTransform.init(rotationAngle: .pi)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label3.transform = CGAffineTransform(rotationAngle: 2 * .pi)
            }) { (true) in
                print("spin")
            }
        case 2:
            if label3.frame.minX < self.view.center.x {
                UIView.animate(withDuration: 1.0) {
                    self.label3.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label3.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }) { (true) in
                    print("slide >")
                }
            }else{
                UIView.animate(withDuration: 1.0) {
                    self.label3.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label3.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }) { (true) in
                    print("slide <")
                }
            }
            
            
         default: print("error")
        }
        
    }
    @objc func touched4(_ tap: UITapGestureRecognizer){
        //let sender = tap.view as! UILabel
        //print(sender.text!)
        switch Int.random(in: 0...2)
        {
        case 0:
            UIView.animate(withDuration: 1.0) {
                self.label4.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label4.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }) { (true) in
                print("size")
            }
        case 1:
            UIView.animate(withDuration: 1.0) {
                self.label4.transform = CGAffineTransform.init(rotationAngle: .pi)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label4.transform = CGAffineTransform(rotationAngle: 2 * .pi)
            }) { (true) in
               print("spin")
            }
        case 2:
            if label4.frame.minX < self.view.center.x {
                UIView.animate(withDuration: 1.0) {
                    self.label4.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label4.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }) { (true) in
                    print("slide >")
                }
            }else{
                UIView.animate(withDuration: 1.0) {
                    self.label4.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label4.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }) { (true) in
                    print("slide <")
                }
            }
            
            
        default: print("error")
        }
        
    }
    @objc func touched5(_ tap: UITapGestureRecognizer){
        //let sender = tap.view as! UILabel
        //print(sender.text!)
        switch Int.random(in: 0...2)
        {
        case 0:
            UIView.animate(withDuration: 1.0) {
                self.label5.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label5.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }) { (true) in
              print("size")
            }
        case 1:
            UIView.animate(withDuration: 1.0) {
                self.label5.transform = CGAffineTransform.init(rotationAngle: .pi)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label5.transform = CGAffineTransform(rotationAngle: 2 * .pi)
            }) { (true) in
                print("spin")
            }
        case 2:
            if label5.frame.minX < self.view.center.x {
                UIView.animate(withDuration: 1.0) {
                    self.label5.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label5.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }) { (true) in
                    print("slide >")
                }
            }else{
                UIView.animate(withDuration: 1.0) {
                    self.label5.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label5.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }) { (true) in
                    print("slide <")
                }
            }
            
            
        default: print("error")
        }
        
    }
    @objc func touched6(_ tap: UITapGestureRecognizer){
        //let sender = tap.view as! UILabel
        //print(sender.text!)
        switch Int.random(in: 0...2)
        {
        case 0:
            UIView.animate(withDuration: 1.0) {
                self.label6.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label6.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }) { (true) in
               print("size")
            }
        case 1:
            UIView.animate(withDuration: 1.0) {
                self.label6.transform = CGAffineTransform.init(rotationAngle: .pi)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label6.transform = CGAffineTransform(rotationAngle: 2 * .pi)
            }) { (true) in
               print("spin")
            }
        case 2:
            if label6.frame.minX < self.view.center.x {
                UIView.animate(withDuration: 1.0) {
                    self.label6.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label6.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }) { (true) in
                    print("slide >")
                }
            }else{
                UIView.animate(withDuration: 1.0) {
                    self.label6.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label6.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }) { (true) in
                    print("slide <")
                }
            }
            
            
        default: print("error")
        }
        
    }
    @objc func touched7(_ tap: UITapGestureRecognizer){
        //let sender = tap.view as! UILabel
        //print(sender.text!)
        switch Int.random(in: 0...2)
        {
        case 0:
            UIView.animate(withDuration: 1.0) {
                self.label7.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label7.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }) { (true) in
                
            }
        case 1:
            UIView.animate(withDuration: 1.0) {
                self.label7.transform = CGAffineTransform.init(rotationAngle: .pi)
            }
            UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                self.label7.transform = CGAffineTransform(rotationAngle: 2 * .pi)
            }) { (true) in
                
            }
        case 2:
            if label7.frame.minX < self.view.center.x {
                UIView.animate(withDuration: 1.0) {
                    self.label7.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label7.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }) { (true) in
                    print("slide >")
                }
            }else{
                UIView.animate(withDuration: 1.0) {
                    self.label7.transform = CGAffineTransform.init(translationX: -12.0, y: 0.0)
                }
                UIView.animate(withDuration: 2.0, delay: 1.0, options: .allowAnimatedContent, animations: {
                    self.label7.transform = CGAffineTransform.init(translationX: 12.0, y: 0.0)
                }) { (true) in
                    print("slide <")
                }
            }
            
            
            
         default: print("error")
        }
        
    }
    
    @objc func panned0(_ pan: UIPanGestureRecognizer){
        self.view.bringSubviewToFront(self.label0)
        let move = pan.translation(in: self.view)
        self.label0.center = CGPoint.init(x: self.label0.center.x + move.x, y: self.label0.center.y + move.y)
        pan.setTranslation(.zero, in: self.view)
    }
    
    @objc func panned1(_ pan: UIPanGestureRecognizer){
        self.view.bringSubviewToFront(self.label1)
        let move = pan.translation(in: self.view)
        self.label1.center = CGPoint.init(x: self.label1.center.x + move.x, y: self.label1.center.y + move.y)
        pan.setTranslation(.zero, in: self.view)
    }
    
    @objc func panned2(_ pan: UIPanGestureRecognizer){
        self.view.bringSubviewToFront(self.label2)
        let move = pan.translation(in: self.view)
        self.label2.center = CGPoint.init(x: self.label2.center.x + move.x, y: self.label2.center.y + move.y)
        pan.setTranslation(.zero, in: self.view)
    }
    
    @objc func panned3(_ pan: UIPanGestureRecognizer){
        self.view.bringSubviewToFront(self.label0)
        let move = pan.translation(in: self.view)
        self.label3.center = CGPoint.init(x: self.label3.center.x + move.x, y: self.label3.center.y + move.y)
        pan.setTranslation(.zero, in: self.view)
    }
    
    @objc func panned4(_ pan: UIPanGestureRecognizer){
        self.view.bringSubviewToFront(self.label4)
        let move = pan.translation(in: self.view)
        self.label4.center = CGPoint.init(x: self.label4.center.x + move.x, y: self.label4.center.y + move.y)
        pan.setTranslation(.zero, in: self.view)
    }
    
    @objc func panned5(_ pan: UIPanGestureRecognizer){
        self.view.bringSubviewToFront(self.label5)
        let move = pan.translation(in: self.view)
        self.label5.center = CGPoint.init(x: self.label5.center.x + move.x, y: self.label5.center.y + move.y)
        pan.setTranslation(.zero, in: self.view)
    }
    
    @objc func panned6(_ pan: UIPanGestureRecognizer){
        self.view.bringSubviewToFront(self.label6)
        let move = pan.translation(in: self.view)
        self.label6.center = CGPoint.init(x: self.label6.center.x + move.x, y: self.label6.center.y + move.y)
        pan.setTranslation(.zero, in: self.view)
    }
    
    @objc func panned7(_ pan: UIPanGestureRecognizer){
        self.view.bringSubviewToFront(self.label7)
        let move = pan.translation(in: self.view)
        self.label7.center = CGPoint.init(x: self.label7.center.x + move.x, y: self.label7.center.y + move.y)
        pan.setTranslation(.zero, in: self.view)
    }
    
    
    @objc func pressed(_ longpress: UILongPressGestureRecognizer){
        let sender = longpress.view as! UILabel
        print(sender.text!)

    }

    @IBAction func animateTextView(_ sender: Any) {
        self.animateButton.setTitleColor(UIColor.init(red: CGFloat.random(in: 0...256)/256, green: CGFloat.random(in: 0...256)/256, blue: CGFloat.random(in: 0...256)/256, alpha: 1), for: .normal)
        UIView.animate(withDuration: 1.5, delay: .zero, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseIn, animations: {
            let x = CGFloat.random(in: 90...UIScreen.main.bounds.size.width-self.textView.frame.width)
            let y = CGFloat.random(in: self.textView.frame.height...UIScreen.main.bounds.size.height-self.textView.frame.height)
            //let width = UIScreen.main.bounds.size.width - x
            //let height = UIScreen.main.bounds.size.height - y
//            print("Screen \(UIScreen.main.bounds.size.width) \(UIScreen.main.bounds.size.height)")
//            print("Text Position \(x) \(y)")
//            print("Text Size \(width) \(height)")
            self.textView.center = CGPoint.init(x: x, y: y)
            //self.textView.frame = CGRect(x: x, y: y, width: width, height: height)
            //self.textView.bounds = CGRect(x: x, y: y, width: width, height: height)
            let angle = ((CGFloat.random(in: -90.0...90.0) * .pi) / 180)
            self.textView.transform = CGAffineTransform(rotationAngle: angle)
            //print("Angle Degrees \(angle)")
            let color = UIColor.init(red: CGFloat.random(in: 0.0...1.0), green: CGFloat.random(in: 0.0...1.0), blue: CGFloat.random(in: 0.0...1.0), alpha: 1)
            self.textView.textColor = color
            //print("View Color is \(color)")
    
        }) { (true) in
            let strings = ["Super Special","Wow","Great Job","Incredible","Amazing","You Are The Best","Fantastic","Another One","Keep Going","Don't Stop Now"]
            //let index = Int.random(in: 1 ... strings.count)
            self.textView.text = "\(strings.randomElement()!)!\nRandom Animation \(self.random)"
            self.textView.sizeToFit()
            self.random += 1
            }
        UIView.animate(withDuration: 2.0, delay: 0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: .curveEaseOut, animations: {
            let x = CGFloat.random(in: self.label0.frame.width...UIScreen.main.bounds.size.width-self.label0.frame.width)
            let y = CGFloat.random(in: self.label0.frame.height...UIScreen.main.bounds.size.height-self.label0.frame.height)
            //let width = UIScreen.main.bounds.size.width - x
            //let height = UIScreen.main.bounds.size.height - y
            self.label0.center = CGPoint(x: x, y: y)
            let angle = ((CGFloat.random(in: -90.0...90.0) * .pi) / 180)
            self.label0.transform = CGAffineTransform(rotationAngle: angle)
            let color = UIColor.init(red: CGFloat.random(in: 0.0...1.0), green: CGFloat.random(in: 0.0...1.0), blue: CGFloat.random(in: 0.0...1.0), alpha: 1)
            self.label0.textColor = color
        }) { (true) in
            switch Int.random(in: 0...2) {
            case 0: self.label0.text = "LabelZero"
            case 1: self.label0.text = "0"
            default: self.label0.text = "NilLabel"
            }
            self.label0.sizeToFit()
        }
        UIView.animate(withDuration: 2.1, delay: 0.1, usingSpringWithDamping: 0.2, initialSpringVelocity: 1.0, options: .allowAnimatedContent, animations: {
            let x = CGFloat.random(in: self.label1.frame.width...UIScreen.main.bounds.size.width-self.label1.frame.width)
            let y = CGFloat.random(in: self.label1.frame.height...UIScreen.main.bounds.size.height-self.label1.frame.height)
            //let width = UIScreen.main.bounds.size.width - x
            //let height = UIScreen.main.bounds.size.height - y
            self.label1.center = CGPoint(x: x, y: y)
            let angle = ((CGFloat.random(in: -90.0...90.0) * .pi) / 180)
            self.label1.transform = CGAffineTransform(rotationAngle: angle)
            let color = UIColor.init(red: CGFloat.random(in: 0.0...1.0), green: CGFloat.random(in: 0.0...1.0), blue: CGFloat.random(in: 0.0...1.0), alpha: 1)
            self.label1.textColor = color
        }) { (true) in
            switch Int.random(in: 0...2) {
            case 0: self.label1.text = "LabelOne"
            case 1: self.label1.text = "1"
            default: self.label1.text = "1stLabel"
            }
            self.label1.sizeToFit()
        }
        UIView.animate(withDuration: 2.2, delay: 0.2, usingSpringWithDamping: 0.4, initialSpringVelocity: 2.0, options: .beginFromCurrentState, animations: {
            let x = CGFloat.random(in: self.label2.frame.width...UIScreen.main.bounds.size.width-self.label2.frame.width)
            let y = CGFloat.random(in: self.label2.frame.height...UIScreen.main.bounds.size.height-self.label2.frame.height)
            //let width = UIScreen.main.bounds.size.width - x
            //let height = UIScreen.main.bounds.size.height - y
            self.label2.center = CGPoint(x: x, y: y)
            let angle = ((CGFloat.random(in: -90.0...90.0) * .pi) / 180)
            self.label2.transform = CGAffineTransform(rotationAngle: angle)
            let color = UIColor.init(red: CGFloat.random(in: 0.0...1.0), green: CGFloat.random(in: 0.0...1.0), blue: CGFloat.random(in: 0.0...1.0), alpha: 1)
            self.label2.textColor = color
        }) { (true) in
            switch Int.random(in: 0...2) {
            case 0: self.label2.text = "LabelTwo"
            case 1: self.label2.text = "2"
            default: self.label2.text = "2ndLabel"
            }
            self.label2.sizeToFit()
        }
        UIView.animate(withDuration: 2.3, delay: 0.3, usingSpringWithDamping: 0.6, initialSpringVelocity: 3.0, options: .transitionFlipFromTop, animations: {
            let x = CGFloat.random(in: self.label3.frame.width...UIScreen.main.bounds.size.width-self.label3.frame.width)
            let y = CGFloat.random(in: self.label3.frame.height...UIScreen.main.bounds.size.height-self.label3.frame.height)
            //let width = UIScreen.main.bounds.size.width - x
            //let height = UIScreen.main.bounds.size.height - y
            self.label3.center = CGPoint(x: x, y: y)
            let angle = ((CGFloat.random(in: -90.0...90.0) * .pi) / 180)
            self.label3.transform = CGAffineTransform(rotationAngle: angle)
            let color = UIColor.init(red: CGFloat.random(in: 0.0...1.0), green: CGFloat.random(in: 0.0...1.0), blue: CGFloat.random(in: 0.0...1.0), alpha: 1)
            self.label3.textColor = color
        }) { (true) in
            switch Int.random(in: 0...2) {
            case 0: self.label3.text = "LabelThree"
            case 1: self.label1.text = "3"
            default: self.label3.text = "3rdLabel"
            }
            self.label3.sizeToFit()
        }
        UIView.animate(withDuration: 2.4, delay: 0.4, usingSpringWithDamping: 0.8, initialSpringVelocity: 4.0, options: .curveEaseInOut, animations: {
            let x = CGFloat.random(in: self.label4.frame.width...UIScreen.main.bounds.size.width-self.label4.frame.width)
            let y = CGFloat.random(in: self.label4.frame.height...UIScreen.main.bounds.size.height-self.label4.frame.height)
            //let width = UIScreen.main.bounds.size.width - x
            //let height = UIScreen.main.bounds.size.height - y
            self.label4.center = CGPoint(x: x, y: y)
            let angle = ((CGFloat.random(in: -90.0...90.0) * .pi) / 180)
            self.label4.transform = CGAffineTransform(rotationAngle: angle)
            let color = UIColor.init(red: CGFloat.random(in: 0.0...1.0), green: CGFloat.random(in: 0.0...1.0), blue: CGFloat.random(in: 0.0...1.0), alpha: 1)
            self.label4.textColor = color
        }) { (true) in
            switch Int.random(in: 0...2) {
            case 0: self.label4.text = "LabelFour"
            case 1: self.label4.text = "4"
            default: self.label4.text = "4thLabel"
            }
            self.label4.sizeToFit()
        }
        UIView.animate(withDuration: 2.5, delay: 0.5, usingSpringWithDamping: 1.0, initialSpringVelocity: 5.0, options: .curveLinear, animations: {
            let x = CGFloat.random(in: self.label5.frame.width...UIScreen.main.bounds.size.width-self.label5.frame.width)
            let y = CGFloat.random(in: self.label5.frame.height...UIScreen.main.bounds.size.height-self.label5.frame.height)
            //let width = UIScreen.main.bounds.size.width - x
            //let height = UIScreen.main.bounds.size.height - y
            self.label5.center = CGPoint(x: x, y: y)
            let angle = ((CGFloat.random(in: -90.0...90.0) * .pi) / 180)
            self.label5.transform = CGAffineTransform(rotationAngle: angle)
            let color = UIColor.init(red: CGFloat.random(in: 0.0...1.0), green: CGFloat.random(in: 0.0...1.0), blue: CGFloat.random(in: 0.0...1.0), alpha: 1)
            self.label5.textColor = color
        }) { (true) in
            switch Int.random(in: 0...2) {
            case 0: self.label5.text = "LabelFive"
            case 1: self.label5.text = "5"
            default: self.label5.text = "5thLabel"
            }
            self.label5.sizeToFit()
        }
        UIView.animate(withDuration: 2.6, delay: 0.6, usingSpringWithDamping: 1.2, initialSpringVelocity: 6.0, options: .transitionCrossDissolve, animations: {
            let x = CGFloat.random(in: self.label6.frame.width...UIScreen.main.bounds.size.width-self.label6.frame.width)
            let y = CGFloat.random(in: self.label6.frame.height...UIScreen.main.bounds.size.height-self.label6.frame.height)
            //let width = UIScreen.main.bounds.size.width - x
            //let height = UIScreen.main.bounds.size.height - y
            self.label6.center = CGPoint(x: x, y: y)
            let angle = ((CGFloat.random(in: -90.0...90.0) * .pi) / 180)
            self.label6.transform = CGAffineTransform(rotationAngle: angle)
            let color = UIColor.init(red: CGFloat.random(in: 0.0...1.0), green: CGFloat.random(in: 0.0...1.0), blue: CGFloat.random(in: 0.0...1.0), alpha: 1)
            self.label6.textColor = color
        }) { (true) in
            switch Int.random(in: 0...2) {
            case 0: self.label6.text = "LabelSix"
            case 1: self.label6.text = "6"
            default: self.label6.text = "6thLabel"
            }
            self.label6.sizeToFit()
        }
        UIView.animate(withDuration: 2.7, delay: 0.7, usingSpringWithDamping: 1.4, initialSpringVelocity: 7.0, options: .layoutSubviews, animations: {
            let x = CGFloat.random(in: self.label7.frame.width...UIScreen.main.bounds.size.width-self.label7.frame.width)
            let y = CGFloat.random(in: self.label7.frame.height...UIScreen.main.bounds.size.height-self.label7.frame.height)
            //let width = UIScreen.main.bounds.size.width - x
            //let height = UIScreen.main.bounds.size.height - y
            self.label7.center = CGPoint(x: x, y: y)
            let angle = ((CGFloat.random(in: -90.0...90.0) * .pi) / 180)
            self.label7.transform = CGAffineTransform(rotationAngle: angle)
            let color = UIColor.init(red: CGFloat.random(in: 0.0...1.0), green: CGFloat.random(in: 0.0...1.0), blue: CGFloat.random(in: 0.0...1.0), alpha: 1)
            self.label7.textColor = color
        }) { (true) in
            switch Int.random(in: 0...2) {
            case 0: self.label7.text = "LabelSeven"
            case 1: self.label7.text = "7"
            default: self.label7.text = "7thLabel"
            }
            self.label7.sizeToFit()
        }

       
    }
    
    @IBAction func resetView(_ sender: Any) {
        
        UIView.animate(withDuration: 2.5, delay: 0.0, options: .beginFromCurrentState, animations: {
            self.label0.frame = self.resetFrames[0]
            self.label0.text = self.resetText[0]
            self.label0.textColor = UIColor.black
            self.label1.frame = self.resetFrames[1]
            self.label1.text = self.resetText[1]
            self.label1.textColor = UIColor.black
            self.label2.frame = self.resetFrames[2]
            self.label2.text = self.resetText[2]
            self.label2.textColor = UIColor.black
            self.label3.frame = self.resetFrames[3]
            self.label3.text = self.resetText[3]
            self.label3.textColor = UIColor.black
            self.label4.frame = self.resetFrames[4]
            self.label4.text = self.resetText[4]
            self.label4.textColor = UIColor.black
            self.label5.frame = self.resetFrames[5]
            self.label5.text = self.resetText[5]
            self.label5.textColor = UIColor.black
            self.label6.frame = self.resetFrames[6]
            self.label6.text = self.resetText[6]
            self.label6.textColor = UIColor.black
            self.label7.frame = self.resetFrames[7]
            self.label7.text = self.resetText[7]
            self.label7.textColor = UIColor.black
            self.textView.frame = self.resetFrames[8]
            self.textView.text = self.resetText[8]
            self.textView.textColor = UIColor.black
            self.label0.transform = .identity
            self.label1.transform = .identity
            self.label2.transform = .identity
            self.label3.transform = .identity
            self.label4.transform = .identity
            self.label5.transform = .identity
            self.label6.transform = .identity
            self.label7.transform = .identity
            self.textView.transform = .identity
            self.label0.sizeToFit()
            self.label1.sizeToFit()
            self.label2.sizeToFit()
            self.label3.sizeToFit()
            self.label4.sizeToFit()
            self.label5.sizeToFit()
            self.label6.sizeToFit()
            self.label7.sizeToFit()
            self.textView.sizeToFit()
        }) { (true) in
            UIView.animate(withDuration: 0.5, animations: {
                self.label0.frame = self.resetFrames[0]
                self.label0.text = self.resetText[0]
                self.label0.textColor = UIColor.black
                self.label1.frame = self.resetFrames[1]
                self.label1.text = self.resetText[1]
                self.label1.textColor = UIColor.black
                self.label2.frame = self.resetFrames[2]
                self.label2.text = self.resetText[2]
                self.label2.textColor = UIColor.black
                self.label3.frame = self.resetFrames[3]
                self.label3.text = self.resetText[3]
                self.label3.textColor = UIColor.black
                self.label4.frame = self.resetFrames[4]
                self.label4.text = self.resetText[4]
                self.label4.textColor = UIColor.black
                self.label5.frame = self.resetFrames[5]
                self.label5.text = self.resetText[5]
                self.label5.textColor = UIColor.black
                self.label6.frame = self.resetFrames[6]
                self.label6.text = self.resetText[6]
                self.label6.textColor = UIColor.black
                self.label7.frame = self.resetFrames[7]
                self.label7.text = self.resetText[7]
                self.label7.textColor = UIColor.black
                self.textView.frame = self.resetFrames[8]
                self.textView.text = self.resetText[8]
                self.textView.textColor = UIColor.black
                self.label0.transform = .identity
                self.label1.transform = .identity
                self.label2.transform = .identity
                self.label3.transform = .identity
                self.label4.transform = .identity
                self.label5.transform = .identity
                self.label6.transform = .identity
                self.label7.transform = .identity
                self.textView.transform = .identity
                self.label0.sizeToFit()
                self.label1.sizeToFit()
                self.label2.sizeToFit()
                self.label3.sizeToFit()
                self.label4.sizeToFit()
                self.label5.sizeToFit()
                self.label6.sizeToFit()
                self.label7.sizeToFit()
                self.textView.sizeToFit()
            })
        }
        
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

No comments:

Post a Comment

Thank you for reading.
Please share your thoughts.
Be blessed and enjoy life!