View动画i

class ViewController: UIViewController {
    @IBOutlet weak var redView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    }

    @IBAction func didStartClicked(sender: UIButton) {
        
        //动画总时长、通过UIView的一些属性
//        UIView.animateWithDuration(10) {
//            //只对UIView的属性有作用
//            self.redView.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
//            self.redView.backgroundColor = UIColor.greenColor()
////            self.redView.layer.cornerRadius = 100
//        }
        
//        UIView.animateWithDuration(5, animations: { 
//            self.redView.backgroundColor = UIColor.yellowColor()
//            }) { (completion) in
//                print("动画执行完成了")
//        }
        //option: 重复、自动回复,   completion(完成时机)
//        UIView.animateWithDuration(1, delay: 0, options: [.Repeat, .Autoreverse], animations: {
//            self.redView.frame = CGRect(x: 200, y: 200, width: 50, height: 50)
//            
//            }, completion: nil)
        
        //POP
        
        //1. 开始设置动画
        UIView.beginAnimations("first", context: nil)
        
        //2. 设置动画属性
        UIView.setAnimationDuration(10)
        UIView.setAnimationRepeatCount(2)
        UIView.setAnimationDelegate(self) //设置代理方法所在的对象
        UIView.setAnimationWillStartSelector(#selector(animateWillStart(_:context:)))
        UIView.setAnimationDidStopSelector(#selector(animateDidStop(_:finished:context:)))
        
        //3. 设置目标属性
        self.redView.backgroundColor = UIColor.greenColor()
        
        //4. 提交动画,开始执行
        UIView.commitAnimations()
    }

    //animationWillStart:(NSString *)animationID context:(void *)context
    func animateWillStart(animationID: NSString, context: UnsafePointer) {
        print("will start")
    }
    
    //NSNumber
    func animateDidStop(animationID: NSString, finished: Bool, context: UnsafePointer) {
        print(finished)
    }

你可能感兴趣的:(View动画i)