酱油08-动画

三种实现动画效果的方法

1. CALayer(CATransaction)

import UIKit

class ViewController: UIViewController {

    var redview : redView!
    var layer : CALayer!
    var shapeLayer: CAShapeLayer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        layer = CALayer()
        layer.frame = CGRect(x: 200, y: 100, width: 100, height: 100)
        layer.backgroundColor = UIColor.brownColor().CGColor
        self.view.layer.addSublayer(layer)
        //CALayer中自带绘图功能
//        shapeLayer = CAShapeLayer()
//        shapeLayer.frame = self.view.bounds
//        shapeLayer.strokeColor = UIColor.blackColor().CGColor
//        shapeLayer.fillColor = UIColor.clearColor().CGColor
//        self.view.layer.addSublayer(shapeLayer)
//        
    }

1.1 点击按钮方法

    @IBAction func Animation(sender: UIButton) {
        //开始设置CALayer动画的属性
        CATransaction.begin()
        //设置动画的运行时间
        CATransaction.setAnimationDuration(5)
        //设置动画出现的效果(kCAMediaTimingFunctionEaseIn)
        CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn))
        //设置动画所需要的效果
        layer.frame = CGRect(x: 300, y: 200, width: 50, height: 50)
        layer.backgroundColor = UIColor.redColor().CGColor
        //开始执行动画
        CATransaction.commit()
    }

2. UIView中自带的方法(animateWithDuration)

import UIKit

class ViewController: UIViewController {

    var redview : redView!
    var layer : CALayer!
    var shapeLayer: CAShapeLayer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        redview = redView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
        redview.backgroundColor = UIColor.redColor()
        self.view.addSubview(redview)
     
    }

2.1 点击按钮方法

    @IBAction func Animation(sender: UIButton) {
        //animateWithDuration方法
        
        UIView.animateWithDuration(5) {
            self.redview.backgroundColor = UIColor.brownColor()
            self.redview.frame = CGRect(x: 200, y: 200, width: 100, height: 100)
        }
        //两种不同的方法
        UIView.animateWithDuration(5, delay: 0, options: .AllowUserInteraction, animations: {
            self.redview.backgroundColor = UIColor.yellowColor()
            }) { (true) in
                print("ok")
        }
        
        //设置UIView的动画属性,与animateWithDuration不同,与CALayer相似
        UIView.beginAnimations("first", context: nil)

        UIView.setAnimationDuration(1)
        //设置重复次数
        UIView.setAnimationRepeatCount(100)
        //设置回到起始位置
        UIView.setAnimationRepeatAutoreverses(true)
        
//        UIView.setAnimationDelay(2)
        redview.frame = CGRect(x: 200, y: 200, width: 50, height: 50)
        redview.backgroundColor = UIColor.brownColor()
        
        UIView.commitAnimations()
    }

3. CALayer(CABasicAnimation/CAKeyframeAnimation/CAAnimationGroup)

import UIKit

class ViewController: UIViewController {

    var redview : redView!
    var layer : CALayer!
    var shapeLayer: CAShapeLayer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
     layer = CALayer()
        layer.frame = CGRect(x: 200, y: 100, width: 100, height: 100)
        layer.backgroundColor = UIColor.brownColor().CGColor
        self.view.layer.addSublayer(layer)
    }

3.1 点击按钮方法

    @IBAction func Animation(sender: UIButton) {
        //设置动画移动路径为中心移动
        let ani01 = CABasicAnimation(keyPath: "position")
        //设置绘图大小及路径
        let path = CGPathCreateMutable()
        
        CGPathAddRect(path, nil, CGRect(x: 150, y: 200, width: 200, height: 100))
        shapeLayer.path = path
        //设置动画起点/偏移/终点(有两个即可,右下为正)
        ani01.fromValue = NSValue(CGPoint: CGPoint(x: 100, y: 100))
        ani01.byValue = NSValue(CGPoint: CGPoint(x: 200, y: 100))
        ani01.toValue = NSValue(CGPoint: CGPoint(x: 300, y: 300))
        ani01.duration = 5
        layer.addAnimation(ani01, forKey: "ani01")
        //设置动画移动时颜色变化
        let ani02 = CABasicAnimation(keyPath: "backgroundColor")
        ani02.fromValue = UIColor.blueColor().CGColor
        ani02.toValue = UIColor.redColor().CGColor
        //设置动画在路径上移动时颜色变化
        let ani03 = CAKeyframeAnimation(keyPath: "position")
        ani03.path = path
    
        //设置变化的颜色
        ani03.values = [
            UIColor.redColor().CGColor,
            UIColor.blackColor().CGColor,
            UIColor.greenColor().CGColor
        ]
        //设置变化时间
        ani03.keyTimes = [0,0.5,0.7]
        ani02.duration = 5
        layer.addAnimation(ani02, forKey: "ani02")
        //设置动画组(实现多种动画)
        let anis = CAAnimationGroup()
        anis.animations = [ani01, ani02, ani03]
        anis.duration = 5
        layer.addAnimation(anis, forKey: "anis")
        
    }

你可能感兴趣的:(酱油08-动画)