Swift学习之路

CABasicAnimation(核心动画)简单实用--(一)

2017-05-15  本文已影响0人  Just丶Go

一 介绍下 CABasicAnimation 的属性

1.keyPath --> 要做动画的属性(此属性是有固定的选择的,不可以随意设置)

/* Animations implement the same property model as defined by CALayer.

包括以下属性

1.transform.scale 比例转换

2.transform.scale.x 宽度的比例转换

3.transform.scale.y 高的比例转换

4.transform.rotation.z 平面图的旋转

5.opacity 透明度

6.margin 边框间距? (这个属性笔者设置过,但是无效,望有设置过且有效的朋友分享下代码)

backgroundColor = 背景色

cornerRadius = layer的角度

borderWidth = 边框宽度

contents = 内容

bounds = 大小

contentsRect = 内容矩形

frame = 位置

hidden = 隐藏

mask = 标记

maskToBounds

position = 位置

shadowOffset = 阴影偏移?

shadowColor = 阴影颜色

shadowRadius = 阴影角度

  1. fromValue, toValue

fromValue = 起始位置 若keyPath属性为position 那么起始点的位置默认为 视图中心点 即anchorPoint(0.5, 0.5) , 改变 锚点 也会改变动画效果

toValue 结束位置 若keypath为position 同上

3.duration 动画播放时间

4.repeatCount 重复播放次数 注:无限次 MAXFLOAT 此属性不可以和 repeatDuration 同时使用

5.repeatDuration 指定动画重复播放多久, 在此时间内,动画会一直重复播放,直到达到设定时间.不可以和repeatCount 同时使用

6.autoreverses 当你设定这个属性为 YES 时,在它到达目的地之后,动画会从结束位置的状态回到初始位置的状态,代替了直接跳转到 开始的值(default is NO)

7.speed 默认值1.0 如果你改变这个值为 2.0,动画会用 2 倍的速度播放。 这样的影响就是使持续时间减半。如果你指定的持续时间为 6 秒,速度为 2.0,动画就会播放 3 秒钟---一半的 持续时间 durationTime = speed * animationTime

下面粘上部分动画效果代码

//MARK: 组合动画
    func startGroupAnimation(animationView: UIView) -> Void {
        //界限
        let boundsAnimation = CABasicAnimation.init(keyPath: "bounds")
        boundsAnimation.fromValue = NSValue.init(cgRect: testView.bounds)
        boundsAnimation.toValue = NSValue.init(cgRect: CGRect.zero)
        //透明度变化
        let opacityAnimation = CABasicAnimation.init(keyPath: "opacity")
        opacityAnimation.fromValue = NSNumber.init(value: 1.0)
        opacityAnimation.toValue = NSNumber.init(value: 0.0)
        // 位移变化
        let animation = CABasicAnimation.init(keyPath: "position")
        animation.fromValue = NSValue.init(cgPoint: testView.layer.position)
        var toValue = testView.layer.position
        toValue.x += 360
        animation.toValue = NSValue.init(cgPoint: toValue)
        //旋转动画
        let rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
        rotationAnimation.toValue = NSNumber.init(value: (2 * Double.pi) * 3)
        rotationAnimation.duration = 3.0
        rotationAnimation.autoreverses = true
        rotationAnimation.repeatCount = MAXFLOAT
        rotationAnimation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseOut)
        //缩放动画
        let scaleAnimation = CABasicAnimation.init(keyPath: "transform.scale")
        scaleAnimation.fromValue = NSNumber.init(value: 0.0)
        scaleAnimation.toValue = NSNumber.init(value: 1.0)
        scaleAnimation.duration = 3.0
        scaleAnimation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseOut)
        //组合动画
        let groupAnimation = CAAnimationGroup.init()
        groupAnimation.duration = 3.0
        groupAnimation.autoreverses = true
        groupAnimation.repeatCount = Float(NSNotFound)
        groupAnimation.animations = [boundsAnimation, rotationAnimation, scaleAnimation, animation, opacityAnimation]
        animationView.layer.add(groupAnimation, forKey: "groupAnimation")
    }
上一篇 下一篇

猜你喜欢

热点阅读