iOS开发 - 扩展UIView实现视图动画

在Swift中,可以快速对UIView进行扩展来实现视图的动画效果

///扩展UIView动画效果
extension UIView {
    
    //动画类型
    enum AnimationType {
        ///旋转
        case transform
        ///翻转
        case flip
        ///透明度
        case alpha
        ///升落
        case updown
        ///拉伸
        case tensile
        /// 上、下
        case up,down
    }
    
    ///增加动画效果
    func ex_animation(type: AnimationType) {
        switch type {
        case .transform:
            let originalTransform = self.transform
            UIView.animateWithDuration(1.5, animations: {
                self.transform = CGAffineTransformMakeRotation(4.0)
            }) { (finished) in
                
                UIView.animateWithDuration(1.5, animations: {
                    self.transform = originalTransform
                })
            }
            
        case .alpha:
            UIView.animateWithDuration(2, animations: {
                self.alpha = 0.3
                }, completion: { (finished) in
                    self.alpha = 1
            })
            
        case .flip:
            UIView.transitionWithView(self, duration: 2, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
                
                }, completion: { (finished) in
                    UIView.transitionWithView(self, duration: 2, options: .TransitionFlipFromRight, animations: {
                        
                        }, completion: nil)
            })
            
        case .updown:
            let originalPoint = self.center
            let point = CGPoint(x: self.center.x, y: self.center.y - 200)
            
            UIView.animateWithDuration(0.3, animations: {
                self.center = point
                }, completion: { (finished) in
                    UIView.animateWithDuration(0.7, animations: {
                        self.center = originalPoint
                    })
            })
            
        case .tensile:
            let originalBounds = self.bounds
            let rect = CGRect(x: 0, y: 0, width: self.frame.width*3, height: self.frame.height*1.2)

            UIView.animateWithDuration(1, animations: {
                self.bounds = rect
                }, completion: { (finished) in
                    UIView.animateWithDuration(1, animations: {
                        self.bounds = originalBounds
                    })
            })
            
        case .up:
            //let originalPoint = self.center
            let point = CGPoint(x: self.center.x, y: self.center.y - 200)
            
            UIView.animateWithDuration(0.3, animations: {
                self.center = point
                }, completion: { (finished) in
                    UIView.animateWithDuration(0.7, animations: {
                        //self.center = originalPoint
                        self.alpha = 0
                    })
            })
            
        case .down:
            //let originalPoint = self.center
            let point = CGPoint(x: self.center.x, y: self.center.y + 200)
            self.alpha = 0.5

            UIView.animateWithDuration(0.2, animations: {
                self.center = point
                }, completion: { (finished) in
                    UIView.animateWithDuration(0.3, animations: {
                        // self.center = originalPoint
                        self.alpha = 1
                    })
            })
        }
    }
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容