使用泛手势的交互式viewController

时间:2016-06-18 23:37:33

标签: swift

每个人都一直在试图找到解决方案来寻找交互式视图控制器转换的解决方案,你可以在向下的方向使用平移手势从顶部到底部带来一个全屏视图控制器。有没有人跑过或创建过这样的代码。以下是我的代码。我已经有了解雇手势,但无法通过向下滑动屏幕来弄清楚如何呈现视图控制器。请帮助!!!

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

}

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

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
}

}

  class PresentAnimator: NSObject {

}

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

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



}

}

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

}

    import UIKit

class DismissAnimator:NSObject { }

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

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

       }

}

1 个答案:

答案 0 :(得分:1)

如果你想在UIViewControllers之间切换一个简单的Pan Gesture,你可以查看:

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

如果你想要它是互动的,就像你可以在风险投资之间来回走动而不必完成整个过渡,我建议你看一下:

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

如果你想更进一步并且有一个自定义的解雇动画,那么请看看:

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