iOS10 动画探索

这几天看熊大的爱鲜蜂,十分佩服熊大的代码,其中最让我魔怔的就是图标跳来跳去。
于是乎我就开始研究代码里的跳跳动画

func playBounceAnimation(_ icon : UIImageView) {
let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
bounceAnimation.values = [1.0 ,1.4, 0.9, 1.15, 0.95, 1.02, 1.0]
bounceAnimation.duration = TimeInterval(0.6)
bounceAnimation.calculationMode = kCAAnimationCubic
icon.layer.add(bounceAnimation, forKey: "bounceAnimation")
}

这就是熊大的跳跳代码
这时候,我发现一个好有趣的东西,在IOS7之后,加入了一个好玩的API
UIView.animateKeyframe
于是乎我在想我能不能用这个新玩意实现熊大的跳跳
于是我开始自杀式的看文档、撸代码
于是出了以下的东西

func scaleWithKeyAnimation(AnimationView:UIView,Duration:TimeInterval,delay:TimeInterval,Value:[CGFloat],relativeDuration:[TimeInterval],mapfortime:TimeInterval){
        UIView.animateKeyframes(withDuration: Duration, delay: delay, options: .calculationModeCubic, animations: {
            var StartTime:TimeInterval = 0
            for i in 0...Value.count - 1{
                if i == 0 {
                    StartTime = 0
                }
                    else
                {
                    StartTime = StartTime + (relativeDuration[i] / mapfortime ) * Duration
                }
                UIView.addKeyframe(withRelativeStartTime: StartTime, relativeDuration: (relativeDuration[i] / mapfortime ) * Duration, animations: {
                    AnimationView.layer.transform.m11 = Value[i]
                    AnimationView.layer.transform.m22 = Value[i]
                })
            }

        })
    }

利用变换矩阵做的m11 和 m22分别是X缩放和Y缩放
嗯、搞定收工
上个对比图


Untitled.gif

左侧是我写的,右侧是熊大写的,还是熊大的精简!!

顺便放上参数

ScaleWithKeyAnimation(AnimationView: imgView, Duration: 0.7, delay: 0, Value: [1.4, 0.9, 1.15, 0.95, 1.02, 1.0],relativeDuration:[0.2,0.4,1.6,2.4,3.2,6.4],mapfortime:6)

论代码,服熊大!!

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

推荐阅读更多精彩内容