8

Sto utilizzando uno UISplitViewController, con un MasterViewController e DetailViewController, senza UINavigationController s.Come rendere l'animazione Segue orizzontale senza UINavigationController?

currenly l'animazione segue per master-> dettaglio, innescato da

performSegueWithIdentifier("showDetail", sender: self) 

consiste nel DetailViewController mostra dal basso verso l'alto.

Come posso creare quell'animazione che mostra i reparti di sinistra?

+0

Puoi aggiungere uno screenshot dello storyboard e lo snippet di codice che stai utilizzando per chiamare 'performSegueWithIdentifier (" showDetail ", mittente: self)'? –

risposta

8

ho di recente avevo bisogno di avere più controllo su come venivano eseguiti i segui, quindi ho creato le mie lezioni personalizzate che eseguono tutte la transizione in direzioni diverse. Ecco una delle implementazioni:

Swift 2.x

override func perform() { 

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

    let firstClassView = self.sourceViewController.view 
    let secondClassView = self.destinationViewController.view 

    let screenWidth = UIScreen.mainScreen().bounds.size.width 
    let screenHeight = UIScreen.mainScreen().bounds.size.height 

    secondClassView.frame = CGRectMake(screenWidth, 0, screenWidth, screenHeight) 

    if let window = UIApplication.sharedApplication().keyWindow { 

     window.insertSubview(secondClassView, aboveSubview: firstClassView) 

     UIView.animateWithDuration(0.4, animations: {() -> Void in 

      firstClassView.frame = CGRectOffset(firstClassView.frame, -screenWidth, 0) 
      secondClassView.frame = CGRectOffset(secondClassView.frame, -screenWidth, 0) 

      }) {(Finished) -> Void in 

       self.sourceViewController.navigationController?.pushViewController(self.destinationViewController, animated: false) 

     } 

    } 

} 

Questo avrà un "da destra a sinistra" transizione. È possibile modificare questa funzione in base alle proprie esigenze semplicemente cambiando le posizioni iniziale e finale del controller della vista di origine e di destinazione.

Inoltre, non dimenticare che è necessario contrassegnare i tuoi passi come "seguiti personalizzati" e assegnargli la nuova classe.

UPDATE: Aggiunto Swift 3 versione

Swift 3

override func perform() { 

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

    let firstClassView = self.source.view 
    let secondClassView = self.destination.view 

    let screenWidth = UIScreen.main.bounds.size.width 
    let screenHeight = UIScreen.main.bounds.size.height 

    secondClassView?.frame = CGRect(x: screenWidth, y: 0, width: screenWidth, height: screenHeight) 

    if let window = UIApplication.shared.keyWindow { 

     window.insertSubview(secondClassView!, aboveSubview: firstClassView!) 

     UIView.animate(withDuration: 0.4, animations: {() -> Void in 

      firstClassView?.frame = (firstClassView?.frame.offsetBy(dx: -screenWidth, dy: 0))! 
      secondClassView?.frame = (secondClassView?.frame.offsetBy(dx: -screenWidth, dy: 0))! 

     }, completion: {(Finished) -> Void in 

      self.source.navigationController?.pushViewController(self.destination, animated: false) 

     }) 

    } 

} 
+0

Works per me in Swift 3, ho dovuto apportare molte correzioni suggerite dal compilatore. – MarksCode

+0

Grazie. Ho aggiunto anche la versione Swift 3 ora. Saluti – Armin

3

Incorpora i controller di visualizzazione in UINavigationControllers.

Per il modello SplitViewController: enter image description here

Sui dispositivi più piccoli si sta andando ad avere per usare navigationController del Maestro.

Inoltre questa domanda è stato risposto here e here e here

più dalla View Controller Programming Guide:

Ci sono due modi per visualizzare un sullo schermo View Controller: incorporarlo in un controller di vista contenitore o presentarlo. I controller vista contenitore forniscono la navigazione principale di un'applicazione ....

Negli storyboard non è così difficile incorporare qualcosa in un controller di navigazione. Fai clic sul controller della vista che desideri incorporare, quindi su Editor-> embed in> controller di navigazione.

+0

non è possibile senza un UINavigationController? –

+0

Qual è la riluttanza? Se non vuoi il navBar, dì solo self.navigationController.navBarHidden = true in viewViewController's viewDidLoad – beyowulf

+0

Ho appena inserito un UINavigationController tra MasterViewController e DetailViewController, ma mostra ancora dal basso –

3

Sembra che il nuovo controller di visualizzazione si presenti modalmente. Se si incorpora il detailViewController in un UINavigationController e si preme il nuovo controller, esso si animerà da destra a sinistra e dovrebbe mostrare anche un pulsante indietro per impostazione predefinita.

+1

Ho appena inserito un UINavigationController tra MasterViewController e DetailViewController, ma mostra ancora dal basso –

1

Quando si dispone di uno schermo compatto larghezza, "Mostra Dettagli" segue ricadere modale segue automatically.So tua DetailViewController apparirà verticalmente come segue predefinita modale.

È possibile utilizzare un UIViewControllerTransitioningDelegate per personalizzare l'animazione di un passaggio modale.

Ecco un esempio per realizzare l'animazione orizzontale:
1. impostare il transitioningDelegate e del metodo delegato

class MasterViewController: UITableViewController,UIViewControllerTransitioningDelegate { 

//prepare for segue 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "showDetail" { 
     let detailVC = segue.destinationViewController as! DetailViewController 
     detailVC.transitioningDelegate = self 
//  detailVC.detailItem = object//Configure detailVC 
    } 
} 

//UIViewControllerTransitioningDelegate 
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
    return LeftTransition() 
} 

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
    let leftTransiton = LeftTransition() 
    leftTransiton.dismiss = true 
    return leftTransiton 
} 

}  

2: un UIViewControllerAnimatedTransitioning personalizzato: LeftTransition

import UIKit 

class LeftTransition: NSObject ,UIViewControllerAnimatedTransitioning { 
var dismiss = false 

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { 
    return 2.0 
} 

    func animateTransition(transitionContext: UIViewControllerContextTransitioning){ 
    // Get the two view controllers 
    let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)! 
    let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)! 
    let containerView = transitionContext.containerView()! 

    var originRect = containerView.bounds 
    originRect.origin = CGPointMake(CGRectGetWidth(originRect), 0) 

    containerView.addSubview(fromVC.view) 
    containerView.addSubview(toVC.view) 

    if dismiss{ 
     containerView.bringSubviewToFront(fromVC.view) 
     UIView.animateWithDuration(transitionDuration(transitionContext), animations: {() -> Void in 
       fromVC.view.frame = originRect 
      }, completion: { (_) -> Void in 
       fromVC.view.removeFromSuperview() 
       transitionContext.completeTransition(true) 
     }) 
    }else{ 
     toVC.view.frame = originRect 
     UIView.animateWithDuration(transitionDuration(transitionContext), 
      animations: {() -> Void in 
       toVC.view.center = containerView.center 
      }) { (_) -> Void in 
       fromVC.view.removeFromSuperview() 
       transitionContext.completeTransition(true) 
     } 
    } 
} 
} 
3

- Swift 3.0 -

soluzione di Armin adattato per SWIFT 3. Nuovo -> File -> Cocoa Touch Class -> Class: ... (Sottoclasse: UIStoryboardSegue).

import UIKit 

class SlideHorSegue: UIStoryboardSegue { 
    override func perform() { 

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

     let firstClassView = self.source.view 
     let secondClassView = self.destination.view 

     let screenWidth = UIScreen.main.bounds.size.width 
     let screenHeight = UIScreen.main.bounds.size.height 

     secondClassView?.frame = CGRect(x: screenWidth, y: 0, width: screenWidth, height: screenHeight) 

     if let window = UIApplication.shared.keyWindow { 

      window.insertSubview(secondClassView!, aboveSubview: firstClassView!) 

      UIView.animate(withDuration: 0.4, animations: {() -> Void in 

       firstClassView?.frame = (firstClassView?.frame)!.offsetBy(dx: -screenWidth, dy: 0) 
       secondClassView?.frame = (secondClassView?.frame)!.offsetBy(dx: -screenWidth, dy: 0) 

      }) {(Finished) -> Void in 

       self.source.navigationController?.pushViewController(self.destination, animated: false) 

      } 

     } 

    } 

} 

Nello storyboard: contrassegna i tuoi passi come "personalizzati" e assegnandogli la nuova classe.

Nota: se si dispone di un UIScrollView nei dettagli VC, questo non funzionerà.

Problemi correlati