2016-06-18 3 views
0

все, что я вырывал свои волосы, пытаясь найти решение для перехода на контроллер интерактивного представления, где вы используете жест панорамы в нисходящем направлении, чтобы вывести контроллер полного экрана сверху вниз дно. Кто-нибудь запускал или создавал любой такой код. Ниже мой код. У меня уже есть жест отбрасывания, но не могу понять, как представить контроллер вида, прокручивая экран. ПОЖАЛУЙСТА ПОМОГИ!!!interactive viewController using pan Gesture

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 { функ animationControllerForDismissedController (уволенный: 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 

класс DismissAnimator: NSObject { }

расширение DismissAnimator: UIViewControllerAnimatedTransitioning { функ transitionDuration (transitionContext: UIViewControllerContextTransitioning) -> NSTime Интервал { возвращение 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()) 
     } 
    ) 

     } 

}

+0

ли вы когда-нибудь получить эту работу ива пытался выяснить это себя – user6520705

ответ

1

Если вы хотите простой жест панорамирования для переключения между UIViewControllers, вы можете проверить это:

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

Если вы хотите быть интерактивным, так как вы можете идти туда и обратно между венчурными капиталистами, не выполняя весь переход, я предлагаю вам проверить это:

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

Если вы хотите пойти еще дальше и есть пользовательское Dismissing анимации, а затем смотреть не дальше, чем это:

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

Смежные вопросы