首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用平移手势的交互式viewController

使用平移手势的交互式viewController
EN

Stack Overflow用户
提问于 2016-06-19 07:37:33
回答 1查看 1.5K关注 0票数 0

每个人,我一直在努力寻找一种交互式视图控制器过渡的解决方案,在这种过渡中,您可以使用向下的平移手势,从上到下实现全屏视图控制器。有没有人遇到或创建过这样的代码。下面是我的代码。我已经有了解除手势,但不知道如何通过在屏幕上向下滑动来呈现视图控制器。请帮帮我!

代码语言:javascript
复制
 import UIKit    
 class ViewController: UIViewController {

let interactor = Interactor()
   var interactors:Interactor? = nil
    let Mview = ModalViewController()
let mViewT: ModalViewController?  = nil
var presentedViewControllers: UIViewController?

override func viewDidLoad() {
    Mview.transitioningDelegate = self
    Mview.modalPresentationStyle = .FullScreen
}

   @IBAction func cameraSlide(sender: UIPanGestureRecognizer) {



    let percentThreshold:CGFloat = 0.3

    // convert y-position to downward pull progress (percentage)
    let translation = sender.translationInView(Mview.view)
    let verticalMovement = translation.y / UIScreen.mainScreen().bounds.height
    let downwardMovement = fmaxf(Float(verticalMovement), 0.0)
    let downwardMovementPercent = fminf(downwardMovement, 1.0)
    let progress = CGFloat(downwardMovementPercent)

    guard let interactor = interactors else { return }

    switch sender.state {
    case .Began:

        interactor.hasStarted = true
        self.presentViewController(Mview, animated: true, completion: nil)
    case .Changed:
        interactor.shouldFinish = progress > percentThreshold
        interactor.updateInteractiveTransition(progress)
    case .Cancelled:
        interactor.hasStarted = false
        interactor.cancelInteractiveTransition()
    case .Ended:
        interactor.hasStarted = false
        if !interactor.shouldFinish {
            interactor.cancelInteractiveTransition()
        } else {
            interactor.finishInteractiveTransition()
        }        default:
        break
    }
}

}

扩展名: UIViewControllerTransitioningDelegate { func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{UIViewControllerTransitioningDelegate DismissAnimator() }

代码语言:javascript
复制
func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
    return interactor.hasStarted ? interactor : nil
}

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return PresentAnimator()
}
func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
    return interactor.hasStarted ? interactor : nil
}

}

代码语言:javascript
复制
  class PresentAnimator: NSObject {

}

扩展PresentAnimator: UIViewControllerAnimatedTransitioning { func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { return 1.0 }

代码语言:javascript
复制
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    guard

        let fromVC2 = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey),
    let toVC2 = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey),
        let containerView2 = transitionContext.containerView() else {return}

    let initialFrame = transitionContext.initialFrameForViewController(fromVC2)
    toVC2.view.frame = initialFrame
    toVC2.view.frame.origin.y = -initialFrame.height * 2


    containerView2.addSubview(fromVC2.view)
    containerView2.addSubview(toVC2.view)

    let screenbounds = UIScreen.mainScreen().bounds
    let Stage = CGPoint(x: 0, y: 0)
    let finalFrame = CGRect(origin: Stage, size: screenbounds.size)

    UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
        toVC2.view.frame = finalFrame
        }, completion: { _ in transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
    }
    )



}

}

代码语言:javascript
复制
   class ModalViewController: UIViewController {

let interactors = Interactor()
var interactor:Interactor? = nil

@IBAction func close(sender: UIButton) {
    dismissViewControllerAnimated(true, completion: nil)
}




@IBAction func handleGesture(sender: UIPanGestureRecognizer) {

    let percentThreshold:CGFloat = 0.3

    // convert y-position to downward pull progress (percentage)
    let translation = sender.translationInView(self.view)
    let verticalMovement = translation.y / -view.bounds.height * 2
    let downwardMovement = fmaxf(Float(verticalMovement), 0.0)
    let downwardMovementPercent = fminf(downwardMovement, 1.0)
    let progress = CGFloat(downwardMovementPercent)

    guard let interactor = interactor else { return }

    switch sender.state {
    case .Began:
        interactor.hasStarted = true
        dismissViewControllerAnimated(true, completion: nil)
    case .Changed:
        interactor.shouldFinish = progress > percentThreshold
        interactor.updateInteractiveTransition(progress)
    case .Cancelled:
        interactor.hasStarted = false
        interactor.cancelInteractiveTransition()
    case .Ended:
        interactor.hasStarted = false
        if !interactor.shouldFinish {
            interactor.cancelInteractiveTransition()
        } else {
            interactor.finishInteractiveTransition()
        }        default:
        break
    }
}

}

代码语言:javascript
复制
    import UIKit

类DismissAnimator: NSObject {}

扩展DismissAnimator : UIViewControllerAnimatedTransitioning { func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { return 1.0 }

代码语言:javascript
复制
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    guard
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey),
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey),
        let containerView = transitionContext.containerView()
        else {
            return
    }

    containerView.insertSubview(toVC.view, belowSubview: fromVC.view)

    let screenBounds = UIScreen.mainScreen().bounds
    let topLeftCorner = CGPoint(x: 0, y: -screenBounds.height * 2)
    let finalFrame = CGRect(origin: topLeftCorner, size: screenBounds.size)

    UIView.animateWithDuration(
        transitionDuration(transitionContext),animations: {fromVC.view.frame = finalFrame},
        completion: { _ in transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        }
    )

       }

}

EN

回答 1

Stack Overflow用户

发布于 2016-06-19 07:47:10

如果你想要一个简单的平移手势在UIViewControllers之间切换,你可以看看这个:

http://www.appcoda.com/custom-segue-animations/

如果你希望它是交互式的,比如你可以在风险投资之间来回切换,而不需要完成整个过渡,我建议你看看这个:

https://www.youtube.com/watch?v=3jAlg5BnYUU

如果你想走得更远,并拥有一个自定义的消除动画,那么只需查看以下内容:

https://www.raywenderlich.com/110536/custom-uiviewcontroller-transitions

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37902579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档