2015-07-27 16 views
5

İki görünüm var Bir tokatlamak stil geçiş geçiş yapmak istiyorum ve bunu yapmak için bir ibne olduğunda bunu yaptım ama burada bir tane yok animasyon sınıfımı nasıl uygulayacağımı bilmiyorum. Benim sınıfta tek şey ise:Özel Görünüm Geçiş Yapmadan Geçiş Yapmak Storyboard segues (Swift)

Ben aşağıdaki özel segue uygulamak uygulamak istediğiniz
let stb = UIStoryboard(name: "Walkthrough", bundle: nil) 
    let walkthrough = stb.instantiateViewControllerWithIdentifier("walk") as! BWWalkthroughViewController 

self.presentViewController(walkthrough, animated: true, completion: nil) 

:

class CustomSegue: UIStoryboardSegue { 

    override func perform() { 
    // Assign the source and destination views to local variables. 
    var firstVCView = self.sourceViewController.view as UIView! 
    var secondVCView = self.destinationViewController.view as UIView! 

    // Get the screen width and height. 
    let screenWidth = UIScreen.mainScreen().bounds.size.width 
    let screenHeight = UIScreen.mainScreen().bounds.size.height 

    // Specify the initial position of the destination view. 
    secondVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight) 

    // Access the app's key window and insert the destination view above the current (source) one. 
    let window = UIApplication.sharedApplication().keyWindow 
    window?.insertSubview(secondVCView, aboveSubview: firstVCView) 

    // Animate the transition. 
    UIView.animateWithDuration(0.2, animations: {() -> Void in 
     firstVCView.frame = CGRectOffset(firstVCView.frame, -screenWidth, 0.0) 
     secondVCView.frame = CGRectOffset(secondVCView.frame, -screenWidth, 0.0) 

     }) { (Finished) -> Void in 

     self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController, 
      animated: false, 
      completion:nil) 
    } 
    } 
} 

Ben herhangi işaretçiler lütfen çalışmak olamıyorum?

+0

Not. Ayrıca, animasyonu 4 saniyeliğine yavaşlatırsanız istenmeyen resimlerin ortaya çıkacağını unutmayın. – SwiftArchitect

cevap

5

olmak, özel geçişler bu şekilde çözebilir "Storyboard segues kullanmaları gerektiğini" ilk cevap ederken:

  1. değiştirme

    Jenerik Yaklaşım senin CustomSeguehemUIStoryboardSegueve benimsemeye UIViewControllerAnimatedTransitioning protokolleri.

  2. Refactor CustomSegue, böylece animasyon her iki protokol tarafından da kullanılabilir.
  3. Kurulum, kendisini olabilir push & pop
  4. özel geçişler sağlamak için navigasyon kontrolörü, bir delegateanimationControllerForOperation oluşturup istediğiniz bir tanımlayıcı ile, CustomSegue bir örneğini dönelim.

Bakış Aşağıda

// Adopt both protocols 
class CustomSegue: UIStoryboardSegue, UIViewControllerAnimatedTransitioning { 

    func animate(firstVCView:UIView, 
     secondVCView:UIView, 
     containerView:UIView, 
     transitionContext: UIViewControllerContextTransitioning?) { 
      // factored transition code goes here 
       }) { (Finished) -> Void in 
        if let context = transitionContext { 
         // UIViewControllerAnimatedTransitioning 
        } else { 
         // UIStoryboardSegue 
        } 
      } 
    } 

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval { 
     // return timing 
    } 

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) { 
     // Perform animate using transitionContext information 
     // (UIViewControllerAnimatedTransitioning) 
    } 

    override func perform() { 
     // Perform animate using segue (self) variables 
     // (UIStoryboardSegue) 
    } 
} 

tam kod örneğidir. Test edilmiştir. Ayrıca, numaralı'un orijinal sorudan birkaç animasyon hatası düzeltdiğine dikkat edin ve animasyonu vurgulamak için bir fade-in efekti ekledim. Eğer `UIScreen.mainScreen()` kullanmaya gerek yoktur ve aslında bazı karışıklıklara olabileceğini


CustomSegue Sınıf

// Animation for both a Segue and a Transition 
class CustomSegue: UIStoryboardSegue, UIViewControllerAnimatedTransitioning { 

    func animate(firstVCView:UIView, 
     secondVCView:UIView, 
     containerView:UIView, 
     transitionContext: UIViewControllerContextTransitioning?) { 

      // Get the screen width and height. 
      let offset = secondVCView.bounds.width 

      // Specify the initial position of the destination view. 
      secondVCView.frame = CGRectOffset(secondVCView.frame, offset, 0.0) 

      firstVCView.superview!.addSubview(secondVCView) 
      secondVCView.alpha = 0; 

      // Animate the transition. 
      UIView.animateWithDuration(self.transitionDuration(transitionContext!), 
       animations: {() -> Void in 
        firstVCView.frame = CGRectOffset(firstVCView.frame, -offset, 0.0) 
        secondVCView.frame = CGRectOffset(secondVCView.frame, -offset, 0.0) 
        secondVCView.alpha = 1; // emphasis 

       }) { (Finished) -> Void in 
        if let context = transitionContext { 
         context.completeTransition(!context.transitionWasCancelled()) 
        } else { 
         self.sourceViewController.presentViewController(
          self.destinationViewController as! UIViewController, 
          animated: false, 
          completion:nil) 
        } 
      } 
    } 

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval { 
     return 4 // four seconds 
    } 

    // Perform Transition (UIViewControllerAnimatedTransitioning) 
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) { 
     self.animate(transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!.view, 
      secondVCView: transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!.view, 
      containerView: transitionContext.containerView(), 
      transitionContext: transitionContext) 
    } 

    // Perform Segue (UIStoryboardSegue) 
    override func perform() { 
     self.animate(self.sourceViewController.view!!, 
      secondVCView: self.destinationViewController.view!!, 
      containerView: self.sourceViewController.view!!.superview!, 
      transitionContext:nil) 
    } 
} 

Sunucu ViewController Sınıf

class ViewController: UIViewController, UINavigationControllerDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.navigationController?.delegate = self 
    } 

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     switch operation { 
     case .Push: 
      return CustomSegue(identifier: "Abc", source: fromVC, destination: toVC) 

     default: 
      return nil 
     } 
    } 
} 
+0

Teşekkür ederim Bunu koduma nasıl uygulayacağımı göremiyorum? – SashaZ

+0

Güzel Lancelot bitti. – BonanzaDriver

+0

Parlak Bunu uygulamak için evde istiyorum. İşten sonra bu gece bir yolculuğum olacak, Şaşırtıcı! – SashaZ

İlgili konular