如何检测平移手势并将其仅应用于顶部子视图

How to detect and apply pan gesture only to top subview

我有一个未知数量的子视图可以出现在名为 mainCanvasView 的主视图上。我的目标是,当用户在子视图上做出平移手势时

  1. 检测它是哪个子视图,并将该子视图置于mainCanvasView
  2. 的顶部
  3. 对该子视图应用平移动画转换

现在,我做了以下事情:

//Added a pan gesture hanlder to mainCanvasView
let panGesture = UIPanGestureRecognizer(target: self, action:#selector(self.handlePanGesture(gesture:)))
self.mainCanvasView.addGestureRecognizer(panGesture)

//Define the method for handling gestures
func handlePanGesture(gesture: UIPanGestureRecognizer) {
    if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed {
        var appliedView = UIView(frame: view.frame)
        viewLooper: for view in self.mainCanvasView.subviews {
            let point = gesture.location(in: view)
            if (view.hitTest(point, with: nil) != nil) { //Find the view that the finger interacted with
                appliedView = view
                break viewLooper
            }
        }

        self.mainCanvasView.bringSubview(toFront: appliedView)
        let translation = gesture.translation(in: view)
        appliedView.transform = (appliedView.transform).translatedBy(x: translation.x, y: translation.y)
        gesture.setTranslation(CGPoint(x: 0, y: 0), in: appliedView)
    }
}

如 gif 所示,虽然移动单个子视图没问题,但当子视图彼此稍微重叠时,会产生一种奇怪的效果,即两个子视图都被移动了。

非常感谢任何关于如何解决此问题的建议。

删除平移手势这是更好的解决方案(您可以在此处添加自己的改进)

我用过touch方法

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for view in self.viewMain.subviews {

        if  let point = touches.first?.location(in: self.view) {

            if let viewTouched =  self.view.hitTest(point, with: event),viewTouched == view {
                viewNew = view
                viewNew.center = point
                self.viewMain.bringSubview(toFront: view)
                print("Tapped")
                break
            } else {
                print("Not Tapped")
            }

        }
    }

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    if  let point =  touches.first?.location(in: self.viewMain) {
        viewNew.center = point
    }

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    viewNew = UIView()
}

viewNew 是全局 UIView 对象

希望对您有所帮助

检查下面的项目它可以得到你想要的输出

已使用 - 在 imageView 上捏合并点击手势并在整个屏幕上移动图像

Link - https://github.com/RockinGarg/Moving-views-.git

触摸处理图像

import UIKit

class objectClass: UIImageView, UIGestureRecognizerDelegate {

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
                let touch: UITouch = touches.first!
        self.center = touch.location(in: self.superview)

    }

}

其他手势功能

//recognizing gestures
func recognizePinchGesture(sender: UIPinchGestureRecognizer) {
    sender.view!.transform = sender.view!.transform.scaledBy(x: sender.scale, y: sender.scale)
    sender.scale = 1
}

func recognizeRotateGesture(sender: UIRotationGestureRecognizer) {
    sender.view!.transform = sender.view!.transform.rotated(by: sender.rotation)
    sender.rotation = 0
}