2014-12-29 18 views
1

我跟着This教程和實現該動畫,但現在我想添加一些功能,如當用戶點擊最小化viewController我想彈出最小化viewController回我試圖實現TapGesture對這種觀點,這是我的代碼:問題與滑出菜單導航在雨燕

import Foundation 
import UIKit 

class TransitionOperator: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate{ 

var snapshot : UIView! 
var isPresenting : Bool = true 

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


func animateTransition(transitionContext: UIViewControllerContextTransitioning) { 
    if isPresenting{ 
     presentNavigation(transitionContext) 
    }else{ 
     dismissNavigation(transitionContext) 
    } 
} 

func presentNavigation(transitionContext: UIViewControllerContextTransitioning) { 
    let container = transitionContext.containerView() 
    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) 
    let fromView = fromViewController!.view 
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) 
    let toView = toViewController!.view 

    let size = toView.frame.size 
    var offSetTransform = CGAffineTransformMakeTranslation(size.width - 120, 0) 
    offSetTransform = CGAffineTransformScale(offSetTransform, 0.6, 0.6) 

    snapshot = fromView.snapshotViewAfterScreenUpdates(true) 

    //TapGesture for detect touch 
    let aSelector : Selector = "singleTap" 
    let tapGesture = UITapGestureRecognizer(target: self, action: aSelector) 
    tapGesture.numberOfTapsRequired = 1 
    self.snapshot.addGestureRecognizer(tapGesture) 

    container.addSubview(toView) 
    container.addSubview(snapshot) 

    let duration = self.transitionDuration(transitionContext) 

    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: nil, animations: { 

     self.snapshot.transform = offSetTransform 

     }, completion: { finished in 

      transitionContext.completeTransition(true) 
    }) 

} 
func singleTap(){ 

    NavigationViewController().dismissViewControllerAnimated(true, completion: nil) 
    println("Touched") 

} 

func dismissNavigation(transitionContext: UIViewControllerContextTransitioning) { 

    let container = transitionContext.containerView() 
    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) 
    let fromView = fromViewController!.view 
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) 
    let toView = toViewController!.view 

    let duration = self.transitionDuration(transitionContext) 

    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: nil, animations: { 

     self.snapshot.transform = CGAffineTransformIdentity 

     }, completion: { finished in 
      transitionContext.completeTransition(true) 
      self.snapshot.removeFromSuperview() 
    }) 
} 

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 

    self.isPresenting = true 
    return self 
} 

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 

    self.isPresenting = false 
    return self 
    } 
} 

當我點擊的是最小化視圖Touched是打印,你可以看到成圖像:

enter image description here

但是查看並沒有被駁回。我想彈出TimelineViewController回來。

在此先感謝。

+0

使用** https://github.com/mutualmobile/MMDrawerController**類創建幻燈片視圖 –

+0

yes.i管理它 –

+0

您是否已經想出瞭解決方案?我有同樣的問題。我也嘗試添加功能,以便用戶可以使用滑動手勢,而不必單擊左上角的菜單欄圖標。 – Mike

回答

0

也許你需要做的是從你的singleTap方法調用self.dismissNavigation()。雖然我不確定什麼上下文傳遞給該方法...

+0

感謝您的回覆,但我應該傳入self.dismissNavigation()? –

0

我知道這已經幾個月了,但我想通了。查看具有參數presented,UIViewController快照的功能animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController)。這是您需要的參考,而不是調用NavigationViewController().,它只是創建新的實例。因此創建一個UIView變量var foo : UIView!animationControllerForPresentedController()集合foo = presented

現在在您的功能singleTap()中,您可以設置foo.dismissViewControllerAnimated()

希望這會有所幫助。