微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如果我将手指移出视野,则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

        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。