iOS 开发 iOS Developer

Swift UI 16 UIView动画效果

2016-08-24  本文已影响0人  前进的苏辰

1.非代码块的动画操作,都是对父视图进行操作的
2.另外3种可以加延迟的动画,原理类似(后面两个参数是闭包,类似于于OC中的block代码块)
1:(UIViewAnimationOptions: 动画过渡效果)
// UIView.animateWithDuration(<#T##duration: NSTimeInterval##NSTimeInterval#>, delay: <#T##NSTimeInterval#>, options: <#T##UIViewAnimationOptions#>, animations: <#T##() -> Void#>, completion: <#T##((Bool) -> Void)?##((Bool) -> Void)?##(Bool) -> Void#>)

2:Spring Animation :
(1)usingSpringWithDamping:它的范围为 0.0f 到 1.0f ,数值越小「弹簧」的振动效果越明显。

(2)initialSpringVelocity:初始的速度,数值越大一开始移动越快。值得注意的是,初始速度取值较高而时间较短时,也会出现反弹情况。

usingSpringWithDamping: 类似弹簧振动效果 0~1
initialSpringVelocity: 初始速度
UIViewAnimationOptions: 动画过渡效果
UIView.animateWithDuration(<#T##duration: NSTimeInterval##NSTimeInterval#>, delay: <#T##NSTimeInterval#>, usingSpringWithDamping: <#T##CGFloat#>, initialSpringVelocity: <#T##CGFloat#>, options: <#T##UIViewAnimationOptions#>, animations: <#T##() -> Void#>, completion: <#T##((Bool) -> Void)?##((Bool) -> Void)?##(Bool) -> Void#>)

3:关键帧动画 : (参数解释如下:)
duration 动画时长
delay 动画延迟
options 动画效果选项
animations 动画执行代码
completion 动画结束执行代码
// UIView.animateKeyframesWithDuration(<#T##duration: NSTimeInterval##NSTimeInterval#>, delay: <#T##NSTimeInterval#>, options: <#T##UIViewKeyframeAnimationOptions#>, animations: <#T##() -> Void#>, completion: <#T##((Bool) -> Void)?##((Bool) -> Void)?##(Bool) -> Void#>)

func createAnimation(){
        
        //  创建一个view视图
        let view = UIView.init(frame: CGRectMake(100, 100, 100, 100))
        view.backgroundColor = UIColor.yellowColor()
        self.view.addSubview(view)
        
        
        //非代码块的动画操作,都是对父视图进行操作的
        
        //开启动画
        UIView.beginAnimations(nil, context: nil)

        //设置动画时间(默认单位为秒 s)
        UIView.setAnimationDuration(1)

        //设置动画启动延迟时间
//        UIView.setAnimationDelay(2)
        
        //设置UIView的动画代理
        UIView.setAnimationDelegate(self)

        //动画结束后执行的操作
        UIView.setAnimationDidStopSelector(#selector(self.stop))
        
        
        //可以改变其子视图的大小、位置、颜色、透明度等等
        //动画的最终结果
        view.frame = self.view.frame
        view.backgroundColor = UIColor.redColor()
        
        //结束动画
        UIView.commitAnimations()
    }

func stop(){

        //创建提示框
        let alertView2 = UIAlertController.init(title: "结束", message: "是否重来", preferredStyle: UIAlertControllerStyle.Alert)

        //让提示框在视图中显示出来
self.presentViewController(alertView2, animated: true, completion: nil)
}
上一篇 下一篇

猜你喜欢

热点阅读