UIView animate with out time duration



当滑动时,我希望在页面之间顺畅地导航(根据手指移动进行更改),而不是在给定的时间内导航

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)
    }
} }

我最近不得不处理它。看看我的github项目,也许会对你有所帮助。

如果是一个果壳。你应该创建类采用

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

并在其中使用手势识别器

最新更新