ios – Swift – 如何制作自定义幻灯片动画?

ios – Swift – 如何制作自定义幻灯片动画?,第1张

概述我一直在寻找快速代码来在视图之间进行简单的自定义幻灯片切换(从左到右或从右到左,没有反d),但我只找到了复杂动画的代码.谢谢大家的帮助! 奥斯卡 我终于在这里找到了答案: http://mathewsanders.com/animated-transitions-in-swift/#custom-transition-animations并稍微加了一下. 1)创建此Swift NSObject文件 我一直在寻找快速代码来在视图之间进行简单的自定义幻灯片切换(从左到右或从右到左,没有反d),但我只找到了复杂动画的代码.谢谢大家的帮助!

奥斯卡

解决方法 我终于在这里找到了答案: http://mathewsanders.com/animated-transitions-in-swift/#custom-transition-animations并稍微加了一下.

1)创建此Swift NSObject文件

class TransitionManager2: NSObject,UIVIEwControllerAnimatedTransitioning,UIVIEwControllerTransitioningDelegate  {    private var presenting = true    // MARK: UIVIEwControllerAnimatedTransitioning protocol methods    // animate a change from one vIEwcontroller to another    func animateTransition(TransitionContext: UIVIEwControllerContextTransitioning) {        // get reference to our fromVIEw,toVIEw and the container vIEw that we should perform the Transition in        let container = TransitionContext.containerVIEw()        let fromVIEw = TransitionContext.vIEwForKey(UITransitionContextFromVIEwKey)!        let toVIEw = TransitionContext.vIEwForKey(UITransitionContextToVIEwKey)!        // set up from 2D transforms that we'll use in the animation        let offScreenRight = CGAffinetransformMakeTranslation(container.frame.wIDth,0)        let offScreenleft = CGAffinetransformMakeTranslation(-container.frame.wIDth,0)        // prepare the toVIEw for the animation        toVIEw.transform = self.presenting ? offScreenRight : offScreenleft        // set the anchor point so that rotations happen from the top-left corner        toVIEw.layer.anchorPoint = CGPoint(x:0,y:0)        fromVIEw.layer.anchorPoint = CGPoint(x:0,y:0)        // updating the anchor point also moves the position to we have to move the center position to the top-left to compensate        toVIEw.layer.position = CGPoint(x:0,y:0)        fromVIEw.layer.position = CGPoint(x:0,y:0)        // add the both vIEws to our vIEw controller        container.addSubvIEw(toVIEw)        container.addSubvIEw(fromVIEw)        // get the duration of the animation        // DON'T just type '0.5s' -- the reason why won't make sense until the next post        // but for Now it's important to just follow this approach        let duration = self.TransitionDuration(TransitionContext)        // perform the animation!        // for this example,just slID both fromVIEw and toVIEw to the left at the same time        // meaning fromVIEw is pushed off the screen and toVIEw slIDes into vIEw        // we also use the block animation usingSpringWithdamPing for a little bounce        UIVIEw.animateWithDuration(duration,delay: 0.0,usingSpringWithdamPing: 1,initialSpringVeLocity: 1,options: nil,animations: {            // slIDe fromVIEw off either the left or right edge of the screen             // depending if we're presenting or dismissing this vIEw            fromVIEw.transform = self.presenting ? offScreenleft : offScreenRight            toVIEw.transform = CGAffinetransformIDentity            },completion: { finished in                // tell our TransitionContext object that we've finished animating                TransitionContext.completeTransition(true)        })    }    // return how many seconds the transiton animation will take    func TransitionDuration(TransitionContext: UIVIEwControllerContextTransitioning) -> NSTimeInterval {        return 0.4    }    // MARK: UIVIEwControllerTransitioningDelegate protocol methods    // return the animataor when presenting a vIEwcontroller    // remmeber that an animator (or animation controller) is any object that aheres to the UIVIEwControllerAnimatedTransitioning protocol    func animationControllerForPresentedController(presented: UIVIEwController,presentingController presenting: UIVIEwController,sourceController source: UIVIEwController) -> UIVIEwControllerAnimatedTransitioning? {        // these methods are the perfect place to set our `presenting` flag to either true or false - voila!        self.presenting = true        return self    }    // return the animator used when dismissing from a vIEwcontroller    func animationControllerFordismissedController(dismissed: UIVIEwController) -> UIVIEwControllerAnimatedTransitioning? {        self.presenting = false        return self        }    }

2)将2个VIEwControllers之间的segue更改为“Custom”

3)在第一个VIEwController中添加以下代码:

let TransitionManager = TransitionManager2()overrIDe func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {    // this gets a reference to the screen that we're about to Transition to    let toVIEwController = segue.destinationVIEwController as! UIVIEwController    // instead of using the default Transition animation,we'll ask    // the segue to use our custom TransitionManager object to manage the Transition animation    toVIEwController.TransitioningDelegate = self.TransitionManager}
总结

以上是内存溢出为你收集整理的ios – Swift – 如何制作自定义幻灯片动画?全部内容,希望文章能够帮你解决ios – Swift – 如何制作自定义幻灯片动画?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1002606.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存