swift--动画效果

一、for循环创建4*4个view,然后对立面的所有view进行动画,这里列集中动画的效果:

1,旋转动画

        for tile in backgrounds{
            
            //现将数字块大小职位原始尺寸的1/10
            tile.layer.setAffineTransform(CGAffineTransform(scaleX: 0.1,y: 0.1))
            
            //设置动画效果,动画时间长度1秒
            UIView.animate(withDuration: 1, delay: 0.01, options: [], animations: { 
                
            }, completion: { (finished) in
               
                UIView.animate(withDuration: 1, animations: { 
                    //完成动画时,数字块复原
                    tile.layer.setAffineTransform(CGAffineTransform.identity)
                })
                
            })
            
        }

2,不透明到透明的效果

        for tile in backgrounds {
            
            tile.alpha = 0
            
            //设置动画效果,动画时间长度1秒
            UIView.animate(withDuration: 1, delay: 0.01, options: [.curveEaseInOut], animations: {
                
            }, completion: { (finished) in
                UIView.animate(withDuration: 1, animations: { 
                    tile.alpha = 1
                })
            })
            
        }

3,从小到大的效果

        for tile in backgrounds {
            
            //现将数字快大小之前原始尺寸的1/10
            tile.layer.setAffineTransform(CGAffineTransform(scaleX:0.1,y:0.1))
            
            //设置动画效果,动画时间长度1秒
            UIView.animate(withDuration: 1, delay: 0.01, options: [], animations: { 
                tile.layer.setAffineTransform(CGAffineTransform(scaleX:1,y:1))
            }, completion: { (finished) in
                UIView.animate(withDuration: 0.08, animations: { 
                    tile.layer.setAffineTransform(CGAffineTransform.identity)
                })
            })
            
        }

记录下三种动画效果

二、使用beginAnimations和commitAnimations方法来实现动画

beginAnimations:此方法开始一个动画快,调用commitAnimations结束一个动画块,并且动画块是允许嵌套的。

commitAnimations:此方法用于结束一个动画块,动画时在一个独立的线程中运行的,动画在生效时,所用应用程序不会中断。

beginAnimations和commitAnimations中间的代码中,我们可以设置各种动画的属性,比如持续时间,使用哪算阈值的动画效果等。

1)淡入、淡出、移动、改变大小动画

func animationAction() {
        //淡出动画
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(2.0)
        imgV.alpha = 0.0
        UIView.commitAnimations()
        
        //淡入动画
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(2.0)
        imgV.alpha = 1.0
        UIView.commitAnimations()
        
        //移动动画
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(2.0)
        imgV.center = CGPoint(x:250,y:250)
        UIView.commitAnimations()
        
        //大小调整动画
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(2.0)
        imgV.frame = CGRect(x:100,y:180,width:50,height:50)
        UIView.commitAnimations()
    }

UIViewAnimationTransition定义了 5 种过渡动画类型:

  • none:无过渡动画效果
  • flipFromLeft:从左侧向右侧翻转
  • flipFromRight:从右侧向左侧翻转
  • curlUp:向上卷数翻页
  • curlDown:向下翻页

代码如下:

 //翻页动画 一 向左翻页
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(2.5)
        UIView.setAnimationTransition(.curlUp, for: self.view1!, cache: true)
        self.view.exchangeSubview(at: 1, withSubviewAt: 0)
        UIView.commitAnimations()
        
        //翻页动画 二 旋转
        UIView.beginAnimations("animation", context: nil)
        UIView.setAnimationDuration(2.0)
        UIView.setAnimationCurve(.easeInOut)
        UIView.setAnimationTransition(.flipFromLeft, for: self.view2!, cache: false)
        self.view.exchangeSubview(at: 1, withSubviewAt: 0)
        UIView.commitAnimations()

跳转到其他页面的操作:

 let EleventhVC = EleventhViewController()
        let windows = UIApplication.shared.delegate?.window
        let nav = UINavigationController(rootViewController:EleventhVC)

        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(2.0)
        UIView.setAnimationCurve(.easeInOut)
        UIView.setAnimationTransition(.curlUp, for: windows!!, cache: true)
        UIView.commitAnimations()
        windows??.rootViewController = nav

主界面做动画:

UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(2.0)
        UIView.setAnimationCurve(.easeOut)
        UIView.setAnimationTransition(.curlDown, for: self.view, cache: false)
        UIView.commitAnimations()