iOS 动画小记

一、旋转动画
func startAnimation(to view: UIView) {
    let anima = CABasicAnimation(keyPath: "transform.rotation")
    anima.toValue = 2 * Double.pi // 旋转角度
    anima.repeatCount = MAXFLOAT // 旋转次数
    anima.isRemovedOnCompletion = false // 动画完成后不移除
    anima.duration = 8 // 旋转时间
    view.layer.add(anima, forKey: nil)
}

二、切换根控制器动画

使用场景:登录成功后,将window的根控制器换为主界面

func loginSuccess() {
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) 
    transition.type = kCATransitionReveal // 动画类型
    transition.subtype = kCATransitionFade 
    let rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()!
    UIApplication.shared.keyWindow?.rootViewController = rootViewController
    UIApplication.shared.keyWindow?.layer.add(transition, forKey: "animation")
}

三、重复动画

UIView.animate(withDuration: 2) { 
    UIView.setAnimationRepeatCount(MAXFLOAT)
    self.animationView.layoutIfNeeded()
}

你可能感兴趣的:(iOS 动画小记)