滑过视图控制器的菜单SWIFT

时间:2015-11-25 19:49:45

标签: ios xcode swift uitableview presentmodalviewcontroller

我对swift和尝试实现触摸菜单有点新手。到目前为止,我已连接视图控制器并使菜单正常工作与此代码我遇到的问题我不希望它推下其他视图控制器。我希望它出现在它上面。下面的代码将所有内容都推倒了。如何修改它以显示在视图控制器上而不是推送。

import UIKit

@objc protocol MenuTransitionManagerDelegate {
    func dismiss()
}

class MenuTransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

    let duration = 0.5
    var isPresenting = false

    var snapshot:UIView? {
        didSet {
            if let delegate = delegate {
                let tapGestureRecognizer = UITapGestureRecognizer(target: delegate, action: "dismiss")
                snapshot?.addGestureRecognizer(tapGestureRecognizer)
            }
        }
    }

    var delegate:MenuTransitionManagerDelegate?

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

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // Get reference to our fromView, toView and the container view
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

        // Set up the transform we'll use in the animation
        guard let container = transitionContext.containerView() else {
            return
        }

        let moveDown = CGAffineTransformMakeTranslation(0, container.frame.height - 240)
        let moveUp = CGAffineTransformMakeTranslation(0, -50)

        // Add both views to the container view
        if isPresenting {
            toView.transform = moveUp
            snapshot = fromView.snapshotViewAfterScreenUpdates(true)
            container.addSubview(toView)
            container.addSubview(snapshot!)
        }

        // Perform the animation
        UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: {

            if self.isPresenting {
                self.snapshot?.transform = moveDown
                toView.transform = CGAffineTransformIdentity
            } else {
                self.snapshot?.transform = CGAffineTransformIdentity
                fromView.transform = moveUp
            }

            }, completion: { finished in

                transitionContext.completeTransition(true)

                if !self.isPresenting {
                    self.snapshot?.removeFromSuperview()
                }
        })
    }

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

        isPresenting = true
        return self
    }

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

        isPresenting = false
        return self
    }

}

2 个答案:

答案 0 :(得分:0)

尝试修改此行。

let moveDown = CGAffineTransformMakeTranslation(0, container.frame.height - 240)
let moveUp = CGAffineTransformMakeTranslation(0, -50)

这些行基本上改变了正在显示/关闭的视图控制器的框架。如果您希望新视图控制器显示在当前视图控制器上方,请尝试更改其Alpha属性。

如果你要改变它的alpha,这里是一个让你了解基本想法的例子。

UIView.animateWithDuration(1, animations: {
  fromView.alpha = 0.3
  toView.alpha = 1
})

答案 1 :(得分:0)

我采取了不同的方法。 在故事板/文档大纲中,确保您的菜单视图遵循您的普通视图,使其位于顶部。

为菜单视图添加约束。

接下来,在尺寸检查器中设置,设置约束,使菜单位于视图上方(即,在可见窗口之外)。

Cntl-从顶部和底部约束拖动到VC以为它们创建出口。

然后将约束值顶部约束的变化设置为0,将底部约束设置为:

frame.size.height - menuheight

(代码或适当时计算)

隐藏它,将顶部约束更改为:

menuHeight * -1

和底部约束:

frame.size.height

左右限制可以显然保持不变。

考虑一下:如果您有一个状态栏和/或导航栏,您可能需要考虑额外的20; 44到等式 - 即显示顶部约束的菜单将是64等,