简体   繁体   English

在特定条件下启用和禁用平移手势

[英]Enabling and Disabling pan gesture on certain condition

我在视图控制器上有三个视图(顶部、中间和底部)。平移手势应用于将上下移动的中间视图。在上升时,顶视图会缩小,底视图会拉伸,反之亦然。

@IBOutlet weak var topViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var bottomViewHeightConstraint: NSLayoutConstraint!
@IBOutlet var panGesture: UIPanGestureRecognizer!

@IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) {
    if sender.state == .began || sender.state == .changed{
        let translation = sender.translation(in: sender.view)
        let changeX = (sender.view?.center.x)!
        let changeY = (sender.view?.center.y)! + translation.y
        topViewHeightConstraint.constant = topViewHeightConstraint.constant + translation.y
        bottomViewHeightConstraint.constant = bottomViewHeightConstraint.constant - translation.y
        sender.view?.center = CGPoint(x: changeX, y: changeY)
        sender.setTranslation(CGPoint.zero, in: sender.view)
    }
}

The above code will decrease the top view's height when the yellow view is dragged upward and increase top view's height on dragging downward.上面的代码将在黄色视图向上拖动时减小顶视图的高度,在向下拖动时增加顶视图的高度。 Vice versa for bottom view as well.反之亦然,底视图也是如此。

Now I want the pan gesture to stop dragging upward if the top view's height is reduced to certain height lets say 200 px but user should be able to drag downward.现在,如果顶视图的高度降低到某个高度,我希望平移手势停止向上拖动,比如说 200 像素,但用户应该能够向下拖动。 Also for bottom view as well user should not go downward if the bottom view's height becomes 200 px.同样对于底视图,如果底视图的高度变为 200 像素,用户也不应该向下。

Anyone please help me with the solution will be greatfull.任何人请帮助我解决方案将非常有用。 Thank You!谢谢你!

Inside panGestureAction function use a logic like this. panGestureAction函数内部使用这样的逻辑。

 @objc func panGestureAction(_ recognizer: UIPanGestureRecognizer) {
        if  your_view_height < 200 && recognizer.translation(in: self.view).y < 0{
            //This will stop moving upwards
            return
        }
  }

Use the same logic with necessary changes for the other case as well.对另一种情况也使用相同的逻辑并进行必要的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM