酱油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")
        
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 3,169评论 1 23
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,580评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,147评论 5 13
  • 显式动画 显式动画,它能够对一些属性做指定的自定义动画,或者创建非线性动画,比如沿着任意一条曲线移动。 属性动画 ...
    清风沐沐阅读 1,981评论 1 5
  • 前言 本文只要描述了iOS中的Core Animation(核心动画:隐式动画、显示动画)、贝塞尔曲线、UIVie...
    GitHubPorter阅读 3,663评论 7 11