Swift 基础动效实现

321 阅读1分钟
     //淡出动画
     UIView.beginAnimations(nil, context: nil)
     UIView.setAnimationDuration(1.0)
     self.floatView.alpha = 0.0
     UIView.commitAnimations()

      //淡入动画
     UIView.beginAnimations(nil, context: nil)
     UIView.setAnimationDuration(1.0)
     self.floatView.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()

/// 动效
UIView.animate(withDuration: 0.3) {
       
             } completion: { _ in
             }
UIViewAnimationTransition定义了 5 种过渡动画类型:

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

 //翻页动画
    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()