【Swift学习笔记-《PRODUCT》读书记录-实现自定义转场动画】
iOS默認(rèn)的push動(dòng)畫(huà)是把即將展示的控制器從右邊推過(guò)來(lái)。有時(shí)我們想實(shí)現(xiàn)類(lèi)似PPT中的一些動(dòng)畫(huà),這時(shí)候就需要自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)了。如下圖我們想實(shí)現(xiàn)一個(gè)淡出并且放大的過(guò)場(chǎng)動(dòng)畫(huà),在退出時(shí)是一個(gè)淡出縮小的動(dòng)畫(huà)。
首先需要自定義一個(gè)類(lèi)DiaryAnimator.swift遵守?UIViewControllerAnimatedTransitioning 協(xié)議,然后實(shí)現(xiàn)它,代碼如下:
import UIKitclass DiaryAnimator: NSObject, UIViewControllerAnimatedTransitioning {// 用于接受外界的operationvar operation:UINavigationControllerOperation!// 返回動(dòng)畫(huà)時(shí)間func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {return 0.4}// 要設(shè)置的動(dòng)畫(huà)//UIKit calls this method when presenting or dismissing a view controller. Use this method to configure the animations associated with your custom transition. You can use view-based animations or Core Animation to configure your animations.func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {let containerview = transitionContext.containerViewlet fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)let fromView = fromVC!.viewlet toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)let toView = toVC?.view// 設(shè)置新場(chǎng)景透明度toView?.alpha = 0.0// 設(shè)置初始值if operation == UINavigationControllerOperation.pop {fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)}else{toView?.transform = CGAffineTransform(scaleX: 0.3, y: 0.3)}containerview.insertSubview(toView!, aboveSubview: fromView!)UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {if self.operation == UINavigationControllerOperation.pop {// 放大要轉(zhuǎn)出的場(chǎng)景fromView?.transform = CGAffineTransform(scaleX: 3.3,y: 3.3)} else {// 設(shè)置新場(chǎng)景為原始大小toView?.transform = CGAffineTransform(scaleX: 1.0,y: 1.0)}toView?.alpha = 1.0},completion: { finished intransitionContext.completeTransition(true)})}}?自定義好動(dòng)畫(huà)后,在要使用的控制器中實(shí)現(xiàn)?UINavigationControllerDelegate 協(xié)議的?optional func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? 方法,返回自定義的動(dòng)畫(huà)即可
extension HomeYearController : UINavigationControllerDelegate {func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {let animate = DiaryAnimator()animate.operation = operationreturn animate} }?
轉(zhuǎn)載于:https://www.cnblogs.com/heyode/p/6513581.html
總結(jié)
以上是生活随笔為你收集整理的【Swift学习笔记-《PRODUCT》读书记录-实现自定义转场动画】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 66-Flutter移动电商实战-会员中
- 下一篇: 【JZOJ3598】【CQOI2014】