iOS动画设计学习iOS动画

iOS动画指南 - 7.简化动画实现的EasyAnimation

2016-09-19  本文已影响1213人  Dariel
本文需要环境及语言: Xcode8.0,swift3.0
EasyAnimation支持OC和swift3.0之前的版本

1. 开篇

在动画实现过程中,一个看似简单的效果往往需要大量的代码,尤其是作用于layer上的动画.为此有了EasyAnimation这个库,这个库可以将Layer Animations写成View Animations的样式.

关于View Animations可以看这篇:iOS动画指南 - 1.View Animations
关于Layer Animations可以看这篇:iOS动画指南 - 2.Layer Animations的基本使用以及iOS动画指南 - 3.Layer Animations的进阶使用

EasyAnimation是老外写的动画库,目前有2K多star.
直达:https://github.com/icanzilb/EasyAnimation

话不多说上代码.例如,要做一个渐变的圆角,以前我们会这样做:

        let cornerRadius = CABasicAnimation(keyPath: "cornerRadius")
        cornerRadius.isRemovedOnCompletion = false
        cornerRadius.fillMode = kCAFillModeBoth
        cornerRadius.fromValue = 0
        cornerRadius.toValue = 20
        cornerRadius.duration = 0.5
        cornerRadius.beginTime = CACurrentMediaTime() + 0.3
        dogImageView.layer.add(cornerRadius, forKey: nil)

用了EasyAnimation可以直接这样:

        UIView.animate(withDuration: 0.5, delay: 0.33, options: [], animations: {
            self.dogImageView.layer.cornerRadius = 20
            }, completion: nil)

2. 基本使用

1. 简单使用
          // 重复 往返 有一个开始和结束的加速度 
        UIView.animate(withDuration: 2.0, delay: 0, options: [.repeat, .autoreverse, .curveEaseInOut], animations: {
            
            // 位置的修改不用EasyAnimation也可以做到
            self.dogImageView.layer.position.x += 180
            
            // 圆角和边框宽度
            self.dogImageView.layer.cornerRadius = 20
            self.dogImageView.layer.borderWidth = 2
            
            // 添加一个3D旋转效果
            var trans3d = CATransform3DIdentity
            trans3d.m34 = -1.0/500.0
            let rotationTransform = CATransform3DRotate(trans3d, CGFloat(-M_PI_4), 0.0, 1.0, 0.0)
            let translationTransform = CATransform3DMakeTranslation(-50.0, 0, 0)
            self.dogImageView.layer.transform = CATransform3DConcat(rotationTransform, translationTransform)
            
        }, completion: nil)

怎么样使用起来是不是要优雅的多啊!用EasyAnimation还有一个好处就是:不会像Layer Animations那样控件位置明明发生了移动,但却还在原先的位置,也就是position.x不是真实的,EasyAnimation就不会有这样的烦恼啦!所有位置都是真实的.

2. 兼容iOS8及之前版本的弹性效果

利用核心动画实现弹性效果这个特性是在iOS9时出来的,如果要使用需要适配iOS9之前的版本.有一个库RBBAnimation可以实现,在EasyAnimation中,iOS9之前是用的RBBAnimation,iOS9是用的CASpringAnimation,所以弹性效果的适配EasyAnimation已经做好啦!

 UIView.animateAndChain(withDuration: 2.0, delay: 0.0, usingSpringWithDamping: 0.25, initialSpringVelocity: 0.0, options: [], animations: {
            
            self.dogImageView.layer.position.x += 180
            self.dogImageView.layer.cornerRadius = 20
            self.dogImageView.layer.borderWidth = 2
        
            self.dogImageView.layer.transform = CATransform3DConcat(
                CATransform3DMakeRotation(CGFloat(-M_PI_2), 0.0, 0.0, 1.0),
                CATransform3DMakeScale(1.33, 1.33, 1.33)
            )
            
            }, completion: nil).animate(withDuration: 2.0, delay: 0.0, options: .repeat, animations: { 
                
                // 回到初始位置,重复执行动画
                self.dogImageView.layer.transform = CATransform3DIdentity
                self.dogImageView.layer.cornerRadius = 0.0
                self.dogImageView.layer.position.x -= 180

                }, completion: nil)

3. 动画组

可能有童鞋注意到了弹性效果回到初始位置的是直接放到了.animate这个闭包中.
EasyAnimation也提供了更加优雅的组动画实现.

        chain = UIView.animateAndChain(withDuration: 1.0, delay: 0.0, options: [], animations: {
            
                    self.dogImageView.center.x += 150.0
            
                }, completion: nil).animate(withDuration: 1.0, animations: {
                
                    self.dogImageView.center.y += 150.0
                
                }).animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
                
                    self.dogImageView.transform = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2))
                
                }, completion: nil).animate(withDuration: 0.5, animations: {
                    
                    self.dogImageView.center.x -= 150.0
                    
                }).animate(withDuration: 2.0, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: .repeat, animations: {
                    
                    self.dogImageView.center.y -= 150.0
                    self.dogImageView.transform = CGAffineTransform.identity
                    
                }, completion: nil)

EasyAnimation充分利用了swift的链式编程特性.相比View Animations的组动画和Layer Animations组动画都简化了不少.

当然这个组动画也提供了终止设定.

        // 需要当前执行的部分停止后才能执行停止这个动作
        delay(seconds: 4.0) { 
            self.chain.cancelAnimationChain()
        }

3. 实现原理

相信有很多童鞋都发现了EasyAnimation的有些方法怎么和系统的这么像.对的,不是像其实就是.它是怎么做到的呢?

fileprivate static func replaceAnimationMethods() {
        //replace actionForLayer...
        method_exchangeImplementations(
            class_getInstanceMethod(self, #selector(UIView.action(for:forKey:))),
            class_getInstanceMethod(self, #selector(UIView.EA_actionForLayer(_:forKey:))))
        
        //replace animateWithDuration...
        method_exchangeImplementations(
            class_getClassMethod(self, #selector(UIView.animate(withDuration:animations:))),
            class_getClassMethod(self, #selector(UIView.EA_animate(withDuration:animations:))))
        method_exchangeImplementations(
            class_getClassMethod(self, #selector(UIView.animate(withDuration:animations:completion:))),
            class_getClassMethod(self, #selector(UIView.EA_animate(withDuration:animations:completion:))))
        method_exchangeImplementations(
            class_getClassMethod(self, #selector(UIView.animate(withDuration:delay:options:animations:completion:))),
            class_getClassMethod(self, #selector(UIView.EA_animate(withDuration:delay:options:animations:completion:))))
        method_exchangeImplementations(
            class_getClassMethod(self, #selector(UIView.animate(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:))),
            class_getClassMethod(self, #selector(UIView.EA_animate(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:))))  
    }

其实EasyAnimation需要导入的库的方法就两个:

// 组动画的两个方法
animateAndChain(withDuration:delay:options: animations:completion:)
animateAndChain(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:)

原理:

EasyAnimation将上面代码放到了UIView的extension中,在EasyAnimation的initialize()初始化方法中调用上面replaceAnimationMethods方法,而replaceAnimationMethods方法是通过使用runtime的方法替换,将自己自定义的方法与系统的方法进行替换.
所以如果在使用cocoapods的情况下,即使不导入EasyAnimation这个库,那么上面的代码也会被执行.

Swift的Runtime和OC的Runtime是一回事吗?

是的. swift的runtime底层其实还是交给OC的Runtime去执行的.我们都知道OC是门动态语言,而Runtime只能在动态语言中才能使用,但swift虽兼顾了动态语言的语法特性,但本质上还是门静态语言.因为有动态语言的语法特性,所以Runtime也是可以使用的.

什么情况下Swift可以进行方法替换?

对于继承于NSObject的类,通过runtime可以获取到的方法,从而可以完成替换.对于没有继承的swift的纯类,就不可以了.继承于NSObject的类为了能够被运行时调用,又不被Swift静态优化,会自动添加@objcdynamic关键字完成动态替换.
具体细节可以看这篇文章:Swift Runtime分析:还像OC Runtime一样吗?

4. 总结:

本篇大致介绍了下EasyAnimation的使用及原理,有兴趣的童鞋不妨去试试,还是挺好用的,作者一直在维护,已经更新到swift3.0了.🙂

本文整理自:iOS.Animations.by.Tutorials.v2.0
源码 : https://github.com/DarielChen/DemoCode
如有疑问,欢迎留言 :-D

上一篇下一篇

猜你喜欢

热点阅读