如何将拖动菜单添加到我的应用程序(类似于控制中心,除了它覆盖了整个屏幕)?

时间:2016-05-20 19:05:25

标签: ios swift

以下代码旨在使隐藏在主视图下方的视图能够完全在主视图上从屏幕底部拖动​​。出于某种原因,下面的代码绝对没有。任何建议都非常感谢。谢谢!

import UIKit

class ControlMenuView: FXBlurView {

   var animator:UIDynamicAnimator!
   var container:UICollisionBehavior!
   var snap: UISnapBehavior!
   var dynamicItem: UIDynamicItemBehavior!
   var gravity:UIGravityBehavior!
   var panGestureRecognizer: UIPanGestureRecognizer!

   func setup () {
        panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(ControlMenuView.handlePan(_:)))
        panGestureRecognizer.cancelsTouchesInView = false

        self.addGestureRecognizer(panGestureRecognizer)

        animator = UIDynamicAnimator(referenceView: self.superview!)
        dynamicItem = UIDynamicItemBehavior(items: [self])
        dynamicItem.allowsRotation = false
        dynamicItem.elasticity = 0

        gravity = UIGravityBehavior(items: [self])
        gravity.gravityDirection = CGVectorMake(0, -1)

        container = UICollisionBehavior(items: [self])

        configureContainer()

        animator.addBehavior(gravity)
        animator.addBehavior(dynamicItem)
        animator.addBehavior(container)

    }

    func configureContainer (){
        let boundaryWidth = UIScreen.mainScreen().bounds.size.width
        container.addBoundaryWithIdentifier("upper", fromPoint: CGPointMake(0, self.frame.size.height), toPoint: CGPointMake(boundaryWidth, self.frame.size.height))

        let boundaryHeight = UIScreen.mainScreen().bounds.size.height
        container.addBoundaryWithIdentifier("lower", fromPoint: CGPointMake(0, boundaryHeight), toPoint: CGPointMake(boundaryWidth, boundaryHeight))


    }

    func handlePan (pan:UIPanGestureRecognizer){
        let velocity = pan.velocityInView(self.superview).y

        var movement = self.frame
        movement.origin.x = 0
        movement.origin.y = movement.origin.y + (velocity * 0.05)

        if pan.state == .Ended {
            panGestureEnded()
        }else if pan.state == .Began {
            snapToTop()
        }else{
            animator.removeBehavior(snap)
            snap = UISnapBehavior(item: self, snapToPoint: CGPointMake(CGRectGetMidX(movement), CGRectGetMidY(movement)))
            animator.addBehavior(snap)
        }

    }

    func panGestureEnded () {
        animator.removeBehavior(snap)

        let velocity = dynamicItem.linearVelocityForItem(self)

        if fabsf(Float(velocity.y)) > 250 {
            if velocity.y < 0 {
                snapToBottom()
            }else{
                snapToTop()
            }
        }else{
            if let superViewHeigt = self.superview?.bounds.size.height {
                if self.frame.origin.y > superViewHeigt / 2 {
                    snapToTop()
                }else{
                    snapToBottom()
                }
            }
        }

    }

    func snapToBottom() {
        gravity.gravityDirection = CGVectorMake(0, 2.5)
    }

    func snapToTop(){
        gravity.gravityDirection = CGVectorMake(0, -2.5)
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.tintColor = UIColor.clearColor()
    }
}

1 个答案:

答案 0 :(得分:0)

  

以下代码旨在使隐藏在下方的视图   主视图可以从屏幕底部拖动   完全超出了主要观点。

您提到视图隐藏在主视图下方。&#34;您是否检查过平移手势目标选择器是否被调用?