2016-06-18 12 views
0

alle ich habe mir die Haare ausreißen versucht, eine Lösung für eine interaktive Ansicht Controller Übergang zu finden, wo Sie die Schwenkgeste in Abwärtsrichtung verwenden, um eine Vollbildansicht Controller von oben auf die zu bringen Unterseite. Hat jemand rüber gelaufen oder einen Code wie diesen erstellt. Unten ist mein Code. Ich habe bereits die Abwurf-Geste gedrückt, kann aber nicht herausfinden, wie man den View-Controller präsentiert, indem man auf dem Bildschirm nach unten wischt. Bitte helfen !!!interaktive AnsichtController mit Schwenk Geste

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

}

Erweiterung Viewcontroller: UIViewControllerTransitioningDelegate { func animationControllerForDismissedController (entlassen: UIViewController) -> UIViewControllerAnimatedTransitioning? { return 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 { 

}

Verlängerungs PresentAnimator: UIViewControllerAnimatedTransitioning { func transitionDuration (transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval { return 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 

Klasse DismissAnimator: NSObject { }

Erweiterung DismissAnimator: UIViewControllerAnimatedTransitioning { func transitionDuration (transitionContext: UIViewControllerContextTransitioning) -> NSTime Intervall { return 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

Haben Sie jemals diese ive zu arbeiten versucht, diese Zahl aus mir heraus – user6520705

Antwort

1

Wenn Sie eine einfache Pan Geste wollen zwischen UIViewControllers wechseln, können Sie überprüfen diese:

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

Wenn Sie es wünschen um interaktiv zu sein, da Sie zwischen VCs hin und her gehen können, ohne den gesamten Übergang zu beenden, schlage ich vor, dass Sie das überprüfen:

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

Wenn Sie wollen noch weiter gehen und eine eigene entlasse Animation haben, dann suchen Sie nicht weiter als diese:

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