在touchesBegan或touchesMoved中取消触摸是否可能?

3
在我的UIViewController的主视图中,我有一个地图视图和另一个视图(我们称之为A视图),它位于地图视图上方。它们的框架都等于self.view.bounds。A视图是一个可调整大小的矩形,类似于用于裁剪图像的矩形。我的目标是让用户指定地图上的区域。因此,我希望用户能够放大和缩小地图,并更改矩形的宽度和高度比例,因为只允许A视图是不可调整大小的正方形会限制它太多。
我从GitHub https://github.com/justwudi/WDImagePicker 上得到了这个项目,我正在使用可调整大小的矩形功能。在Github链接的第二张图片中,有一个带有8个点和阴影区域的矩形。如果用户触摸阴影区域,我想让触摸传递到位于A视图后面的地图上。只有当用户点击点内部的区域或点上时(这样他就想要调整矩形大小),我才希望A视图识别触摸。因此,我修改了对A视图触摸的代码,现在如下:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {

        if cropBorderView.frame.contains(touch.location(in: self)){
            print("touch contains  - touchesbegan")
            //self.isUserInteractionEnabled = true
        }
        else{
            print("Touch does not contain - touchesbegan")
            self.touchesCancelled(touches, with: event)
            //return
        }
        let touchPoint = touch.location(in: cropBorderView)

        anchor = self.calculateAnchorBorder(touchPoint)
        fillMultiplyer()
        resizingEnabled = true
        startPoint = touch.location(in: self.superview)
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("inside touches moved")
    if let touch = touches.first {
        if cropBorderView.frame.contains(touch.location(in: self)){
            print("touch contains - touchesmoved")
            //self.isUserInteractionEnabled = true
        }
        else{
            print("Touch does not contain - touchesmoved ")
            self.touchesCancelled(touches, with: event)
            //return
        }

        if resizingEnabled! {
            self.resizeWithTouchPoint(touch.location(in: self.superview))
        }
    }
}

我确实希望能够在内部和外部单击时识别触摸,但是当我在外部单击时它并没有停止触摸。这意味着调用self.touchesCancelled(touches, with: event)没有起作用。使用return会导致崩溃并且也不起作用。是否有解决此问题的方法?

感谢您的时间和考虑。

1个回答

1

touchesCancelled(_:with:)只是一个UITouch的通知,这样做行不通。 据我所知,您在覆盖层UIView中实现了触摸处理程序,如果是这样,您可以尝试使用UIControl类中的cancelTracking(with:)函数替换对self.touchesCancelled(touches, with: event)的调用implementation

else {
    print("Touch does not contain - touchesmoved ")
    self.cancelTracking(with event)
}

基于 hitTest: 更新解决方案:

我已经检查了可能的解决方案,似乎可以使用 hitTest: 来避免不必要的触摸识别。以下示例是 Swift Playground,您可以轻敲和拖动触摸并查看控制台中发生的情况:

import UIKit
import PlaygroundSupport

class JSGView : UIView {

    var centerView = UIView()

    override func didMoveToSuperview() {
        frame = CGRect(x: 0, y: 0, width: 320, height: 480)
        backgroundColor = UIColor.clear

        centerView.frame = CGRect(x: 110, y: 190, width: 100, height: 100)
        centerView.backgroundColor = UIColor.blue
        addSubview(centerView)
    }

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

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            if (hitTest(touch.location(in: self), with: event) != nil) {
                print("Touch passed hit test and seems valid")
                super.touchesCancelled(touches, with: event)
                return
            }
        }

        print("Touch isn't passed hit test and will be ignored")
        super.touchesMoved(touches, with: event)
    }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if centerView.bounds.contains(centerView.convert(point, from: self)) {
            return centerView
        }
        return nil
    }
}

class JSGViewController : UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
        let customView = JSGView()
        view.addSubview(customView)
    }
}

let controller = JSGViewController()
PlaygroundPage.current.liveView = controller.view

这个函数在我的类中不可用,该类只继承自UIView。当我尝试让它同时继承自UIControl时,会出现错误提示:“从'UIView'和'UIControl'进行多重继承”。 - rgoncalv
修复了上面的问题。刚刚意识到UIControl继承自UIView。然而,调用self.cancelTracking(with event)并没有提供预期的结果。什么都没有发生,就像使用self.touchesCancelled(touches, with: event)一样。 - rgoncalv
1
这段代码在iOS 10.0版本中运行良好,但在iOS 11.0版本中无法正常工作。请提供最新的代码。提前感谢。 - joel prithivi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接