荣耀彩票代理

  • 热门专题

自定义转场动画 Swift3.0版本

作者:  发布日期:2016-12-26 20:23:30
  • 荣耀彩票代理ZHUANCHANGDONGHUAZHEISHI,SHUOJIANDANYEJIANDAN,KEYITONGGUOpresentViewController:animated:completion:HEdismissViewControllerAnimated:completion:ZHEIYIZUHANSHUYIMOTAISHITUDEFANGSHIZHANXIAN、YINZANGSHITU。RUGUOYONGDAOLEnavigationController,HAIKEYIDIAOYONGpushViewController:animated:HEpopViewControllerZHEIYIZUHANSHUJIANGXINDESHITUKONGZHIQIYAZHAN、DANZHAN。

    荣耀彩票代理XIATUZHONGSUOYOUZHUANCHANGDONGHUADOUSHIZIDINGYIDEDONGHUA,ZHEIXIEXIAOGUORUGUOBUYONGZIDINGYIDONGHUAZEHENNANSHENZHIWUFASHIXIAN:


    demo演示

    荣耀彩票代理YOUYULUPINGDEYUANYIN,YOUXIEXIAOGUOWUFAWANQUANZHANXIAN,BIRUTAQISHIHAIZHICHIHENGPING。

    ZIDINGYIZHUANCHANGDONGHUADEXIAOGUOSHIXIANQILAIBIJIAOFUZA,RUGUOJINJINSHIKAOBEIYIFENNENGGOUYUNXINGDEDAIMAQUEBUDONGQIZHONGYUANLI,JIUYOUKENENGDAILAIGEZHONGYINZANGDEbug。BENWENYOUQIANRUSHENJIESHAOXIAMIANJIGEZHISHI:

    传统的基于闭包的实现方式及其缺点自定义present转场动画交互式(Interactive)转场动画转场协调器与UIModalPresentationCustomUINavigationController转场动画

    WOWEIZHEIPIANJIAOCHENGZHIZUOLEYIGEdemo,NINKEYIQUZAIWODEgithubSHANGcloneXIALAI:CustomTransition,RUGUOJUEDEYOUBANGZHUHAIWANGJIGEstarYISHIZHICHI。BENWENYISwift+CHUNDAIMASHIXIAN,DUIYINGDEOC+StoryboardBANBENZAIdemoZHONGYEKEYIZHAODAO,NEISHIPINGGUODEGUANFANGSHIFANDAIMA,ZHENGQUEXINGGENGYOUBAOZHENG。demoZHONGYONGDAOLECocoaPods,NINYEXUXUYAOZHIXINGpod installMINGLINGBINGDAKAI.xcworkspaceWENJIAN。

    荣耀彩票代理ZAIKAISHIZHENGSHIDEJIAOCHENGQIAN,NINSHOUXIANXUYAOXIAZAIdemo,ZAIDAIMAMIANQIANWENZISHICANGBAIDE,demoZHONGBAOHANDEZHUSHIZUYIJIESHIBENWENSUOYOUDEZHISHIDIAN。QICI,NINHAIDELEJIEZHEIJIGEBEIJINGZHISHI。

    From和To

    ZAIDAIMAHEWENZIZHONG,JINGCHANGHUICHUXIANfromViewHEtoView。RUGUOCUOWUDELIJIETAMENDEHANYIHUIDAOZHIDONGHUALUOJIWANQUANCUOWU。fromViewBIAOSHIDANGQIANSHITU,toViewBIAOSHIYAOTIAOZHUANDAODESHITU。RUGUOSHICONGASHITUKONGZHIQIpresentDAOB,ZEASHIfrom,BSHIto。CONGBSHITUKONGZHIQIdismissDAOASHI,BBIANCHENGLEfrom,ASHIto。YONGYIZHANGTUBIAOSHI:


    from和to

    Presented和Presenting

    ZHEIYESHIYIZUXIANGDUIDEGAINIAN,TARONGYIYUfromViewHEtoViewHUNXIAO。JIANDANLAISHUO,TABUSHOUpresentHUOdismissDEYINGXIANG,RUGUOSHICONGASHITUKONGZHIQIpresentDAOB,NEIMEAZONGSHIBDEpresentingViewController,BZONGSHIADEpresentedViewController。

    modalPresentationStyle

    荣耀彩票代理ZHEISHIYIGEMEIJULEIXING,BIAOSHIpresentSHIDONGHUADELEIXING。QIZHONGKEYIZIDINGYIDONGHUAXIAOGUODEZHIYOULIANGZHONG:FullScreenHECustom,LIANGZHEDEQUBIEZAIYUFullScreenHUIYICHUfromView,ERCustomBUHUI。BIRUWENZHANGKAITOUDEgifZHONG,DISANGEDONGHUAXIAOGUOJIUSHICustom。

    基于block的动画

    荣耀彩票代理ZUIJIANDANDEZHUANCHANGDONGHUASHISHIYONGtransitionFromViewControllerFANGFA:


    传统的转场动画实现

    ZHEIGEFANGFASUIRANYIJINGGUOSHI,DANSHIDUITADEFENXIYOUZHUYUHOUMIANZHISHIDELIJIE。TAYIGONGYOU6GECANSHU,QIANLIANGGEBIAOSHICONGNAGEVCKAISHI,TIAOZHUANDAONAGEVC,ZHONGJIANLIANGGECANSHUBIAOSHIDONGHUADESHIJIANHEXUANXIANG。ZUIHOULIANGGECANSHUBIAOSHIDONGHUADEJUTISHIXIANXIJIEHEHUIDIAOBIBAO。

    ZHEILIUGECANSHUQISHIJIUSHIYICIZHUANCHANGDONGHUASUOBIBEIDELIUGEYUANSU。TAMENKEYIFENWEILIANGZU,QIANLIANGGECANSHUWEIYIZU,BIAOSHIYEMIANDETIAOZHUANGUANXI,HOUMIANSIGEWEIYIZU,BIAOSHIDONGHUADEZHIXINGLUOJI。

    荣耀彩票代理ZHEIGEFANGFADEQUEDIANZHIYISHIKEZIDINGYICHENGDUBUGAO(ZAIHOUMIANNINHUIFAXIANNENGZIDINGYIDEBUJINJINSHIDONGHUAFANGSHI),LINGYIGEQUEDIANZESHIZHONGYONGXINGBUHAO,YEKEYISHUOSHIOUHEDUBIJIAODA。

    荣耀彩票代理ZAIZUIHOULIANGGEBIBAOCANSHUZHONG,KEYIYUJIANDESHIfromViewControllerHEtoViewControllerCANSHUDOUHUIBEIYONGDAO,ERQIETAMENSHIDONGHUADEGUANJIAN。JIASHESHITUKONGZHIQIAKEYITIAOZHUANDAOB、C、D、E、F,ERQIETIAOZHUANDONGHUAJIBENXIANGSI,NINHUIFAXIANtransitionFromViewControllerFANGFAYAOBEIFUZHIDUOCI,MEICIZHIHUIXIUGAISHAOLIANGNEIRONG。

    自定义present转场动画

    荣耀彩票代理CHUYUJIEOUHETIGAOKEZIDINGYICHENGDUDEKAOLV,WOMENLAIXUEXIZHUANCHANGDONGHUADEZHENGQUESHIYONGZISHI。

    SHOUXIANYAOLEJIEYIGEGUANJIANGAINIAN:ZHUANCHANGDONGHUADAILI,TASHIYIGESHIXIANLEUIViewControllerTransitioningDelegateXIEYIDEDUIXIANG。WOMENXUYAOZIJISHIXIANZHEIGEDUIXIANG,TADEZUOYONGSHIWEIUIKitTIGONGYIXIAJIGEDUIXIANGZHONGDEYIGEHUODUOGE:

    Animator:

    TASHISHIXIANLEUIViewControllerAnimatedTransitioningXIEYIDEDUIXIANG,YONGYUKONGZHIDONGHUADECHIXUSHIJIANHEDONGHUAZHANSHILUOJI,DAILIKEYIWEIpresentHEdismissGUOCHENGFENBIETIGONGAnimator,YEKEYITIGONGTONGYIGEAnimator。

    JIAOHUSHIAnimator:HEAnimatorLEISI,BUGUOTASHIJIAOHUSHIDE,HOUMIANHUIYOUXIANGXIJIESHAO

    PresentationKONGZHIQI:

    荣耀彩票代理TAKEYIDUIpresentGUOCHENGGENGJIACHEDIDEZIDINGYI,BIRUXIUGAIBEIZHANSHISHITUDEDAXIAO,XINZENGZIDINGYISHITUDENG,HOUMIANHUIYOUXIANGXIJIESHAO。


    转场动画代理

    ZAIZHEIYIXIAOJIEZHONG,WOMENSHOUXIANJIESHAOZUIJIANDANDEAnimator。HUIGUYIXIAZHUANCHANGDONGHUABIBEIDE6GEYUANSU,TAMENBEIFENWEILIANGZU,BICIZHIJIANMEIYOUGUANLIAN。AnimatorDEZUOYONGDENGTONGYUDIERZUDESIGEYUANSU,YEJIUSHISHUODUIYUTONGYIGEAnimator,KEYISHIYONGYUATIAOZHUANB,YEKEYISHIYONGYUATIAOZHUANC。TABIAOSHIYIZHONGTONGYONGDEYEMIANTIAOZHUANSHIDEDONGHUALUOJI,BUSHOUXIANYUJUTIDESHITUKONGZHIQI。

    荣耀彩票代理RUGUONINDUDONGLEZHEIDUANHUA,ZHENGGEZIDINGYIDEZHUANCHANGDONGHUALUOJIJIUHENQINGCHULE,YISHITUKONGZHIQIATIAOZHUANDAOBWEILI:

    创建动画代理,在事情比较简单时,A自己就可以作为代理设置B的transitioningDelegate为步骤1中创建的代理对象调用presentViewController:animated:completion:并把参数animated设置为true系统会找到代理中提供的Animator,由Animator负责动画逻辑

    荣耀彩票代理YONGJUTIDELIZIJIESHIJIUSHI:

    // 这个类相当于A
    class CrossDissolveFirstViewController: UIViewController, UIViewControllerTransitioningDelegate {
        // 这个对象相当于B
        crossDissolveSecondViewController.transitioningDelegate = self  
    
        // 点击按钮触发的函数
        func animationButtonDidClicked() {
            self.presentViewController(crossDissolveSecondViewController, 
                             animated: true, completion: nil)
        }
    
        // 下面这两个函数定义在UIViewControllerTransitioningDelegate协议中
        // 用于为present和dismiss提供animator
        func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) > UIViewControllerAnimatedTransitioning? {
    //        也可以使用CrossDissolveAnimator,动画效果各有不同
    //        return CrossDissolveAnimator()
            return HalfWaySpringAnimator()
        }
    
        func animationControllerForDismissedController(dismissed: UIViewController) > UIViewControllerAnimatedTransitioning? {
            return CrossDissolveAnimator()
        }
    }

    DONGHUADEGUANJIANZAIYUanimatorRUHESHIXIAN,TASHIXIANLEUIViewControllerAnimatedTransitioningXIEYI,ZHISHAOXUYAOSHIXIANLIANGGEFANGFA,WOJIANYININZIXIYUEDUanimateTransitionFANGFAZHONGDEZHUSHI,TASHIZHENGGEDONGHUALUOJIDEHEXIN:

    class HalfWaySpringAnimator: NSObject, UIViewControllerAnimatedTransitioning {
        /// 设置动画的持续时间
        func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) > NSTimeInterval {
            return 2
        }
    
        /// 设置动画的进行方式,附有详细注释,demo中其他地方的这个方法不再解释
        func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
            let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
            let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
            let containerView = transitionContext.containerView()
    
            // 需要关注一下from/to和presented/presenting的关系
            // For a Presentation:
            //      fromView = The presenting view.
            //      toView   = The presented view.
            // For a Dismissal:
            //      fromView = The presented view.
            //      toView   = The presenting view.
    
            var fromView = fromViewController?.view
            var toView = toViewController?.view
    
            // iOS8引入了viewForKey方法,尽可能使用这个方法而不是直接访问controller的view属性
            // 比如在form sheet样式中,我们为presentedViewController的view添加阴影或其他decoration,animator会对整个decoration view
            // 添加动画效果,而此时presentedViewController的view只是decoration view的一个子视图
            if transitionContext.respondsToSelector(Selector("viewForKey:")) {
                fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
                toView = transitionContext.viewForKey(UITransitionContextToViewKey)
            }
    
            // 我们让toview的origin.y在屏幕的一半处,这样它从屏幕的中间位置弹起而不是从屏幕底部弹起,弹起过程中逐渐变为不透明
            toView?.frame = CGRectMake(fromView!.frame.origin.x, fromView!.frame.maxY / 2, fromView!.frame.width, fromView!.frame.height)
            toView?.alpha = 0.0
    
            // 在present和,dismiss时,必须将toview添加到视图层次中
            containerView?.addSubview(toView!)
    
            let transitionDuration = self.transitionDuration(transitionContext)
            // 使用spring动画,有弹簧效果,动画结束后一定要调用completeTransition方法
            UIView.animateWithDuration(transitionDuration, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .CurveLinear, animations: { () > Void in
                toView!.alpha = 1.0     // 逐渐变为不透明
                toView?.frame = transitionContext.finalFrameForViewController(toViewController!)    // 移动到指定位置
                }) { (finished: Bool) > Void in
                    let wasCancelled = transitionContext.transitionWasCancelled()
                    transitionContext.completeTransition(!wasCancelled)
            }
        }
    }

    animateTransitionFANGFADEHEXINZESHICONGZHUANCHANGDONGHUASHANGXIAWENHUOQUBIYAODEXINXIYIWANCHENGDONGHUA。SHANGXIAWENSHIYIGESHIXIANLEUIViewControllerContextTransitioningDEDUIXIANG,TADEZUOYONGZAIYUWEIanimateTransitionFANGFATIGONGBIBEIDEXINXI。NINBUYINGGAIHUANCUNRENHEGUANYUDONGHUADEXINXI,ERSHIYINGGAIZONGSHICONGZHUANCHANGDONGHUASHANGXIAWENZHONGHUOQU(BIRUfromViewHEtoView),ZHEIYANGKEYIBAOZHENGZONGSHIHUOQUDAOZUIXINDE、ZHENGQUEDEXINXI。


    转场动画上下文

    荣耀彩票代理HUOQUDAOZUGOUXINXIHOU,WOMENDIAOYONGUIView.animateWithDurationFANGFABADONGHUAJIAOJICore AnimationCHULI。QIANWANBUYAOWANGJIZAIDONGHUADIAOYONGJIESHUHOU,ZHIXINGcompleteTransitionFANGFA。

    本节的知识在Demo的Cross Dissolve荣耀彩票代理文件夹中有详细的代码。其中有两个animator文件,这说明我们可以为present和dismiss提供同一个animator,或者分别提供各自对应的animator。如果两者动画效果类似,您可以共用同一个animator,惟一的区别在于:

    present时,要把toView加入到container的视图层级。dismiss时,要把fromView从container的视图层级中移除。

    RUGUONINBEIQIANMIANZHEIYIDADUANDAIMAHEZHISHINONGYUNLE,HUOZHEZANSHIYONGBUDAOZHEIXIEJUTIDEZHISHI,NINZHISHAOXUYAOJIZHUZIDINGYIDONGHUADEJIBENYUANLIHELIUCHENG:

    设置将要跳转到的视图控制器(presentedViewController)的transitioningDelegate充当代理的对象可以是源视图控制器(presentingViewController),也可以是自己创建的对象,它需要为转场动画提供一个animator对象。animator对象的animateTransition是整个动画的核心逻辑。

    交互式(Interactive)转场动画

    荣耀彩票代理GANGGANGWOMENSHUODAO,SHEZHILEtoViewControllerDEtransitioningDelegateSHUXINGBINGQIEpresentSHI,UIKitHUICONGDAILICHUHUOQUanimator,QISHIZHEILIHAIYOUYIGEXIJIE:UIKitHAIHUIDIAOYONGDAILIDEinteractionControllerForPresentation:FANGFALAIHUOQUJIAOHUSHIKONGZHIQI,RUGUODEDAOLEnilZEZHIXINGFEIJIAOHUSHIDONGHUA,ZHEIJIUHUIDAOLESHANGYIJIEDENEIRONG。

    RUGUOHUOQUDAOLEBUSHInilDEDUIXIANG,NEIMEUIKitBUHUIDIAOYONGanimatorDEanimateTransitionFANGFA,ERSHIDIAOYONGJIAOHUSHIKONGZHIQI(HAIJIDEQIANMIANJIESHAODONGHUADAILIDESHIYITUME,JIAOHUSHIDONGHUAKONGZHIQIHEanimatorSHIPINGJIGUANXI)DEstartInteractiveTransition:FANGFA。

    荣耀彩票代理SUOWEIDEJIAOHUSHIDONGHUA,TONGCHANGSHIJIYUSHOUSHIQUDONG,CHANSHENGYIGEDONGHUAWANCHENGDEBAIFENBILAIKONGZHIDONGHUAXIAOGUO(WENZHANGKAITOUDEgifZHONGDIERGEDONGHUAXIAOGUO)。ZHENGGEDONGHUABUZAISHIYICIXING、LIANGUANDEWANCHENG,ERSHIZAIRENHESHIHOUDOUKEYIGAIBIANBAIFENBISHENZHIQUXIAO。ZHEIXUYAOYIGESHIXIANLEUIPercentDrivenInteractiveTransitionXIEYIDEJIAOHUSHIDONGHUAKONGZHIQIHEanimatorXIETONGGONGZUO。ZHEIKANSHANGQUSHIYIGEFEICHANGFUZADERENWU,DANUIKitYIJINGFENGZHUANGLEZUGOUDUOXIJIE,WOMENZHIXUYAOZAIJIAOHUSHIDONGHUAKONGZHIQIHEZHONGDINGYIYIGESHIJIANCHULIHANSHU(BIRUCHULIHUADONGSHOUSHI),RANHOUZAIJIESHOUDAOXINDESHIJIANSHI,JISUANDONGHUAWANCHENGDEBAIFENBIBINGQIEDIAOYONGupdateInteractiveTransitionLAIGENGXINDONGHUAJINDUJIKE。

    用下面这段代码简单表示一下整个流程(删除了部分细节和注释,请不要以此为正确参考),完整的代码请参考demo中的Interactivity文件夹:

    // 这个相当于fromViewController
    class InteractivityFirstViewController: UIViewController {
         // 这个相当于toViewController
        lazy var interactivitySecondViewController: InteractivitySecondViewController = InteractivitySecondViewController()
        // 定义了一个InteractivityTransitionDelegate类作为代理
        lazy var customTransitionDelegate: InteractivityTransitionDelegate = InteractivityTransitionDelegate()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupView() // 主要是一些UI控件的布局,可以无视其实现细节
    
            /// 设置动画代理,这个代理比较复杂,所以我们新建了一个代理对象而不是让self作为代理
            interactivitySecondViewController.transitioningDelegate = customTransitionDelegate
        }
    
        // 触发手势时,也会调用animationButtonDidClicked方法
        func interactiveTransitionRecognizerAction(sender: UIScreenEdgePanGestureRecognizer) {
            if sender.state == .Began {
                self.animationButtonDidClicked(sender)
            }
        }
    
        func animationButtonDidClicked(sender: AnyObject) {
            self.presentViewController(interactivitySecondViewController, animated: true, completion: nil)
        }
    }

    FEIJIAOHUSHIDEDONGHUADAILIZHIXUYAOWEIpresentHEdismissTIGONGanimatorJIKE,DANSHIZAIJIAOHUSHIDEDONGHUADAILIZHONG,HAIXUYAOWEIpresentHEdismissTIGONGJIAOHUSHIDONGHUAKONGZHIQI:

    class InteractivityTransitionDelegate: NSObject, UIViewControllerTransitioningDelegate {
        func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) > UIViewControllerAnimatedTransitioning? {
            return InteractivityTransitionAnimator(targetEdge: targetEdge)
        }
    
        func animationControllerForDismissedController(dismissed: UIViewController) > UIViewControllerAnimatedTransitioning? {
            return InteractivityTransitionAnimator(targetEdge: targetEdge)
        }
    
        /// 前两个函数和淡入淡出demo中的实现一致
        /// 后两个函数用于实现交互式动画
    
        func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) > UIViewControllerInteractiveTransitioning? {
            return TransitionInteractionController(gestureRecognizer: gestureRecognizer, edgeForDragging: targetEdge)
        }
    
        func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) > UIViewControllerInteractiveTransitioning? {
            return TransitionInteractionController(gestureRecognizer: gestureRecognizer, edgeForDragging: targetEdge)
        }
    }

    荣耀彩票代理animatorZHONGDEDAIMALVEQU,TAHEFEIJIAOHUSHIDONGHUAZHONGDEanimatorLEISI。YINWEIJIAOHUSHIDEDONGHUAZHISHIYIZHONGJINSHANGTIANHUA,TABIXUZHICHIFEIJIAOHUSHIDEDONGHUA,BIRUZHEIGELIZIZHONG,DIANJIANNIUYIRANCHUFADESHIFEIJIAOHUSHIDEDONGHUA,ZHISHISHOUSHIHUADONGCAIHUICHUFAJIAOHUSHIDONGHUA。

    class TransitionInteractionController: UIPercentDrivenInteractiveTransition {
        /// 当手势有滑动时触发这个函数
        func gestureRecognizeDidUpdate(gestureRecognizer: UIScreenEdgePanGestureRecognizer) {
            switch gestureRecognizer.state {
            case .Began: break
            case .Changed: self.updateInteractiveTransition(self.percentForGesture(gestureRecognizer))  //手势滑动,更新百分比
            case .Ended:    // 滑动结束,判断是否超过一半,如果是则完成剩下的动画,否则取消动画
                if self.percentForGesture(gestureRecognizer) >= 0.5 {
                    self.finishInteractiveTransition()
                }
                else {
                    self.cancelInteractiveTransition()
                }
            default: self.cancelInteractiveTransition()
            }
        }
        private func percentForGesture(gesture: UIScreenEdgePanGestureRecognizer) > CGFloat {
            let percent = 根据gesture计算得出
            return percent
        }
    }

    荣耀彩票代理JIAOHUSHIDONGHUASHIZAIFEIJIAOHUSHIDONGHUADEJICHUSHANGSHIXIANDE,WOMENXUYAOCHUANGJIANYIGEJICHENGZIUIPercentDrivenInteractiveTransitionLEIXINGDEZILEI,BINGQIEZAIDONGHUADAILIZHONGFANHUIZHEIGELEIXINGDESHILIDUIXIANG。

    ZAIZHEIGELEIXINGZHONG,JIANTINGSHOUSHI(HUOZHEXIAZAIJINDUDENGDENG)DESHIJIANBIANHUA,RANHOUDIAOYONGpercentForGestureFANGFAGENGXINDONGHUAJINDUJIKE。

    转场协调器与UIModalPresentationCustom

    ZAIJINXINGZHUANCHANGDONGHUADETONGSHI,NINHAIKEYIJINXINGYIXIETONGBUDE,EWAIDEDONGHUA,BIRUWENZHANGKAITOUgifZHONGDEDISANGELIZI。presentedViewHEpresentingViewKEYIGENGGAIZISHENDESHITUCENGJI,TIANJIAEWAIDEXIAOGUO(YINYING,YUANJIAO)。UIKitSHIYONGZHUANCHENGXIEDIAOQILAIGUANLIZHEIXIEEWAIDEDONGHUA。NINKEYITONGGUOXUYAOCHANSHENGDONGHUAXIAOGUODESHITUKONGZHIQIDEtransitionCoordinatorSHUXINGLAIHUOQUZHUANCHANGXIEDIAOQI,ZHUANCHANGXIEDIAOQIZHIZAIZHUANCHANGDONGHUADEZHIXINGGUOCHENGZHONGCUNZAI。


    转场动画协调器

    XIANGYAOWANCHENGgifZHONGDISANGELIZIDEXIAOGUO,WOMENHAIXUYAOSHIYONGUIModalPresentationStyle.CustomLAIDAITI.FullScreen。YINWEIHOUZHEHUIYICHUfromViewController,ZHEIXIANRANBUFUHEXUQIU。

    DANGpresentDEFANGSHIWEI.CustomSHI,WOMENHAIKEYISHIYONGUIPresentationControllerGENGJIACHEDIDEKONGZHIZHUANCHANGDONGHUADEXIAOGUO。YIGE presentation controllerJUBEIYIXIAJIGEGONGNENG:

    设置presentedViewController的视图大小添加自定义视图来改变presentedView的外观为任何自定义的视图提供转场动画效果根据size class进行响应式布局

    NINKEYIRENWEI,. FullScreenYIJIQITApresentFENGGEDOUSHIswiftWEIWOMENSHIXIANTIGONGHAODE,TAMENSHI.CustomDETELI。ER.CustomYUNXUWOMENGENGJIAZIYOUDEDINGYIZHUANCHANGDONGHUAXIAOGUO。

    UIPresentationControllerTIGONGLESIGEHANSHULAIDINGYIpresentHEdismissDONGHUAKAISHIQIANHOUDECAOZUO:

    presentationTransitionWillBegin: present将要执行时presentationTransitionDidEnd:present执行结束后dismissalTransitionWillBegin:dismiss将要执行时dismissalTransitionDidEnd:dismiss执行结束后

    下面的代码简要描述了gif中第三个动画效果的实现原理,您可以在demo的Custom Presentation荣耀彩票代理文件夹下查看完成代码:

    // 这个相当于fromViewController
    class CustomPresentationFirstViewController: UIViewController {
        // 这个相当于toViewController
        lazy var customPresentationSecondViewController: CustomPresentationSecondViewController = CustomPresentationSecondViewController()
        // 创建PresentationController
        lazy var customPresentationController: CustomPresentationController = CustomPresentationController(presentedViewController: self.customPresentationSecondViewController, presentingViewController: self)
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupView() // 主要是一些UI控件的布局,可以无视其实现细节
    
            // 设置转场动画代理
            customPresentationSecondViewController.transitioningDelegate = customPresentationController
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func animationButtonDidClicked() {
            self.presentViewController(customPresentationSecondViewController, animated: true, completion: nil)
        }
    }

    荣耀彩票代理ZHONGDIANZAIYURUHESHIXIANCustomPresentationControllerZHEIGELEI:

    class CustomPresentationController: UIPresentationController, UIViewControllerTransitioningDelegate {
        var presentationWrappingView: UIView?  // 这个视图封装了原视图,添加了阴影和圆角效果
        var dimmingView: UIView? = nil  // alpha为0.5的黑色蒙版
    
        // 告诉UIKit为哪个视图添加动画效果
        override func presentedView() > UIView? {
            return self.presentationWrappingView
        }
    }
    
    // 四个方法自定义转场动画发生前后的操作
    extension CustomPresentationController {
        override func presentationTransitionWillBegin() {
            // 设置presentationWrappingView和dimmingView的UI效果
            let transitionCoordinator = self.presentingViewController.transitionCoordinator()
            self.dimmingView?.alpha = 0
            // 通过转场协调器执行同步的动画效果
            transitionCoordinator?.animateAlongsideTransition({ (context: UIViewControllerTransitionCoordinatorContext) > Void in
                self.dimmingView?.alpha = 0.5
                }, completion: nil)
        }
    
        /// present结束时,把dimmingView和wrappingView都清空,这些临时视图用不到了
        override func presentationTransitionDidEnd(completed: Bool) {
            if !completed {
                self.presentationWrappingView = nil
                self.dimmingView = nil
            }
        }
    
        /// dismiss开始时,让dimmingView完全透明,这个动画和animator中的动画同时发生
        override func dismissalTransitionWillBegin() {
            let transitionCoordinator = self.presentingViewController.transitionCoordinator()
            transitionCoordinator?.animateAlongsideTransition({ (context: UIViewControllerTransitionCoordinatorContext) > Void in
                self.dimmingView?.alpha = 0
                }, completion: nil)
        }
    
        /// dismiss结束时,把dimmingView和wrappingView都清空,这些临时视图用不到了
        override func dismissalTransitionDidEnd(completed: Bool) {
            if completed {
                self.presentationWrappingView = nil
                self.dimmingView = nil
            }
        }
    }
    
    extension CustomPresentationController {
    }

    除此以外,这个类还要处理子视图布局相关的逻辑。它作为动画代理,还需要为动画提供animator对象,详细代码请在demo的Custom Presentation文件夹下阅读。

    UINavigationController转场动画

    荣耀彩票代理DAOMUQIANWEIZHI,SUOYOUZHUANCHANGDONGHUADOUSHISHIYONGYUpresentHEdismissDE,QISHIUINavigationControllerYEKEYIZIDINGYIZHUANCHANGDONGHUA。LIANGZHESHIPINGXINGGUANXI,HENDUODOUKEYILEIBIGUOLAI:

    class FromViewController: UIViewController, UINavigationControllerDelegate {
       let toViewController: ToViewController = ToViewController()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupView() // 主要是一些UI控件的布局,可以无视其实现细节
    
            self.navigationController.delegate = self
        }
    }

    YUpresent/dismissBUTONGDESHI,XIANZAISHITUKONGZHIQISHIXIANDESHIUINavigationControllerDelegateXIEYI,RANGZIJICHENGWEInavigationControllerDEDAILI。ZHEIGEXIEYILEISIYUCIQIANDEUIViewControllerTransitioningDelegateXIEYI。

    FromViewControllerSHIXIANUINavigationControllerDelegateXIEYIDEJUTICAOZUORUXIA:

    func navigationController(navigationController: UINavigationController, 
         animationControllerForOperation operation: UINavigationControllerOperation, 
                         fromViewController fromVC: UIViewController, 
                             toViewController toVC: UIViewController) 
                            > UIViewControllerAnimatedTransitioning? {
            if operation == .Push {
                return PushAnimator()
            }
            if operation == .Pop {
                return PopAnimator()
            }
            return nil;
        }

    ZHIYUanimator,JIUHECIQIANMEIYOURENHEQUBIELE。KEJIAN,YIGEFENGZHUANGDEHENHAODEanimator,BUJINNENGZAIpresent/dismissSHISHIYONG,SHENZHIHAIKEYIZAIpush/popSHISHIYONG。

    UINavigationControllerYEKEYITIANJIAJIAOHUSHIZHUANCHANGDONGHUA,YUANLIYEHECIQIANLEISI。

    总结

    DUIYUFEIJIAOHUSHIDONGHUA,XUYAOSHEZHIpresentedViewControllerDEtransitioningDelegateSHUXING,ZHEIGEDAILIXUYAOWEIpresentHEdismissTIGONGanimator。ZAIanimatorZHONGGUIDINGLEDONGHUADECHIXUSHIJIANHEBIAOXIANLUOJI。

    荣耀彩票代理DUIYUJIAOHUSHIDONGHUA,XUYAOZAICIQIANDEJICHUSHANG,YOUtransitioningDelegateSHUXINGTIGONGJIAOHUSHIDONGHUAKONGZHIQI。ZAIKONGZHIQIZHONGJINXINGSHIJIANCHULI,RANHOUGENGXINDONGHUAWANCHENGJINDU。

    DUIYUZIDINGYIDONGHUA,KEYITONGGUOUIPresentationControllerZHONGDESIGEHANSHUZIDINGYIDONGHUAZHIXINGQIANHOUDEXIAOGUO,KEYIXIUGAIpresentedViewControllerDEDAXIAO、WAIGUANBINGTONGBUZHIXINGQITADEDONGHUA。

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规