UIView动画没有持续时间

时间:2015-08-04 09:43:02

标签: ios swift

滑动时,我希望在页面之间平滑导航(根据手指移动而改变),不要在给定时间内导航

class FirstCustomSegue: UIStoryboardSegue {
   override func perform() {
    // Assign the source and destination views to local variables.
    var firstVCView = self.sourceViewController.view as UIView!
    var secondVCView = self.destinationViewController.view as UIView!

    // Get the screen width and height.
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    // Specify the initial position of the destination view.
    secondVCView.frame = CGRectMake(screenWidth, 0.0, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.4, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, -screenWidth, 0.0)
        secondVCView.frame = CGRectOffset(secondVCView.frame, -screenWidth, 0.0)

        }) { (Finished) -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                animated: false,
                completion: nil)
    }
} }

1 个答案:

答案 0 :(得分:0)

我最近不得不处理它。看看我的github project,也许它会帮助你。

如果简而言之。你应该创建类采用

  

UIViewControllerAnimatedTransitioning

并实施2种方法。一个用于动画的持续时间,另一个用于自定义动画(移动,淡入淡出等)。

例如:

class ModalPresentAnimationController: NSObject, UIViewControllerAnimatedTransitioning {

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 0.5
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
    // ...
    let duration = transitionDuration(transitionContext)
    UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.2, options: UIViewAnimationOptions.CurveLinear, animations: {
        toVC.view.frame = // new postion
        }) { finished in

           transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
    }
}

然后在ViewController中指定一个新的ViewControllerTransitioning

extension ViewController: UIViewControllerTransitioningDelegate {

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    swipeInteractionController.wireToViewController(presented)
    return modalPresentAnimationController
}

但是,如果你想添加“根据手指动作改变”你应该创建类采用

  

UIPercentDrivenInteractiveTransition

并在其中使用gestureRecognizer。