如果我将手指移出视野,则touchesMoved touches.first!.view保持不变

问题描述

我对touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?)函数有疑问。更具体地说-touchs.first!.view属性。 我需要知道用户指向的视图标签。但是当我将手指拖出视线范围时,touches.first!.view值不会改变。

这就是我在做什么:

enter image description here

我有以下代码可以打印出所选视图的标签和背景色:

override func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) {
    print("\(touches.first!.view!.backgroundColor),View tag: " + String(touches.first!.view!.tag))
}

但是我希望打印出来的是UIExtendedGrayColorSpace 1 1,View tag: 0,但是我得到的却是<UIDynamicSystemColor: 0x600000820b40; name = systemGroupedBackgroundColor>,View tag: 5。这意味着,即使我从一个视图中拖动光标,也仍然会从touches.first!.view中获得第一个单击的视图。

如何获得 当前 选定的视图?

解决方法

根据 Rob 上面的评论回答

override func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) {
    super.touchesMoved(touches,with: event)
    if let touch = touches.first {
        if ( !self.grayUIView.frame.contains(touch.location(in: self.grayUIView)) ){
        return

        }else {
        //still in current view - process with your logic

        }
    }
}