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