Swift中的滑出式菜单导航问题

时间:2014-12-29 07:00:38

标签: ios swift

我遵循了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

提前致谢。

2 个答案:

答案 0 :(得分:0)

也许你需要做的是从你的单一Tap方法调用self.dismiss Navigation()。虽然我不确定传递给该方法的上下文...

答案 1 :(得分:0)

我知道已经过了几个月,但我明白了。查看函数animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController),其中包含参数presented,快照的UIViewController。这是您需要的参考,而不是调用只是创建新实例的NavigationViewController().。因此,在UIView设置var foo : UIView!中创建animationControllerForPresentedController()变量foo = presented

现在,您可以在功能singleTap()中设置foo.dismissViewControllerAnimated()

希望这有帮助。