Swift:动画

import UIKit


class ViewController: UIViewController {

    var myView:UIView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        myView = UIView()

        myView?.frame = CGRect(x:100,y:100,width:100,height:100)

        myView?.backgroundColor = UIColor.green

        self.view.addSubview(myView!)

        

        // Do any additional setup after loading the view, typically from a nib.

    }

    

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

//      简单的过渡动画:后面跟的事尾随闭包

        UIView.animate(withDuration: 1) {

            self.myView?.backgroundColor = UIColor.red

        }

//      嵌套动画:

        UIView.animate(withDuration: 3, animations: {

            self.myView?.center = CGPoint(x:200,y:400)

        }){ (finish) in

            self.myView?.backgroundColor = UIColor.gray

        }

//      延迟动画:

        UIView.animateKeyframes(withDuration: 2, delay: 3, options: [], animations: {

            

        }, completion: nil)

//      阻尼动画:usingSpringWithDamping弹力系数0-10的弹力最大、    initialSpringVelocity初始速度

        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [], animations: {

    

            }, completion: nil)

        

//      UIView层的转场动画:

//     1)、重构视图:

        UIView.transition(with:myView!, duration: 3, options: .transitionCurlUp, animations: {

            self.myView?.backgroundColor = UIColor.yellow

        }) { (finish) in

            

        }

//     1)、从一个视图转到另一个视图

        let otherView = UIView()

        otherView.frame = CGRect(x:200,y:100,width:200,height:300)

        otherView.backgroundColor = UIColor.red

        

        UIView.transition(from:myView!, to: otherView, duration: 3, options: .transitionFlipFromTop, completion: nil)

        

    }

    

    


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值