xcode Swift - 如何制作自定义幻灯片动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31674138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:33:57  来源:igfitidea点击:

Swift - How to do a custom slide animation?

iosxcodeswiftanimationslide

提问by Oscar

I've been looking for swift code to make simple custom slide transitions between views (just left to right or right to left, without bounce) but I only found code for complicated animations. Thanks everyone for your help !

我一直在寻找快速代码来在视图之间进行简单的自定义幻灯片转换(从左到右或从右到左,没有反弹),但我只找到了复杂动画的代码。感谢大家的帮助!

Oscar

奥斯卡

回答by Oscar

I finally found the answer here : http://mathewsanders.com/animated-transitions-in-swift/#custom-transition-animationsand adpated it a little bit.

我终于在这里找到了答案:http: //mathewsanders.com/animated-transitions-in-swift/#custom-transition-animations 并对其进行了一些调整。

1) Create this Swift NSObject file

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) Change the segue between the 2 ViewControllers to "Custom"

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

3) Add in the first ViewController this code :

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

}

回答by Muhammad_Awaab

What you need to do is subclass UIStoryboardSegue Class and override the performmethod.

您需要做的是子类化 UIStoryboardSegue 类并覆盖perform方法。

The code inside your perform method would be something like this

执行方法中的代码将是这样的

    var ourOriginViewController = self.sourceViewController as! UIViewController

    ourOriginViewController.navigationController?.pushViewController(self.destinationViewController as! UIViewController, animated: false)
    var transitionView = ourOriginViewController.navigationController?.view

    UIView.transitionWithView(transitionView!, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: { () -> Void in

    }) { (success) -> Void in

    }

Assign this segue class to your custom segue in storyboard

将此 segue 类分配给故事板中的自定义 segue

Attaching screenshot for referenceenter image description here

附上截图以供参考在此处输入图片说明