【问题标题】:Is it possible to cancel a touch from inside touchesBegan or touchesMoved?是否可以从 touchesBegan 或 touchesMoved 内部取消触摸?
【发布时间】:2016-11-18 21:41:13
【问题描述】:

在 UIViewController 的主视图中,我有一个 mapView 和另一个位于 mapView 上方的视图(假设是视图 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 会导致崩溃并且效果不佳。这个问题有解决办法吗?

感谢您的时间和考虑。

【问题讨论】:

    标签: ios iphone swift uigesturerecognizer uitouch


    【解决方案1】:

    touchesCancelled(_:with:) 只是UITouch 的通知,它不会这样工作。 据我了解,您在叠加层UIView 中实现了触摸处理程序,如果是这样,您可以尝试用UIControlimplementation 中的cancelTracking(with:) 函数替换对self.touchesCancelled(touches, with: event) 的调用:

    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' 多重继承”
    • 修复了上面的问题。刚刚实现 UIControl 继承自 UIView。但是,调用 self.cancelTracking(with event) 并没有提供预期的结果。 self.touchesCancelled(touches, with: event) 什么都没有发生
    • 此代码在 iOS 10.0.... 版本中工作正常,但在 iOS 11.0 版本中不工作....请提供最新代码.....提前谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    相关资源
    最近更新 更多