ios - 获取触摸 UIView 的手指数量

标签 ios iphone swift uiview uitouch

我想根据当前 UIView 上的触摸次数更改 UIView 的颜色。我已经成功地通过一次触摸创建了这个:

class colorChangerViewClass: UIView {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        setColor(color: .blue)
        print("touchesBegan")
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        setColor(color: .green)
        print("touchesEnded")
    }


    func setColor(color: UIColor) {
    self.backgroundColor = color
    }
}

但我在实现多点触控方面遇到了困难。我感觉我在这里有些不明白。

UIView 是否有一个属性来检查当前有多少根手指正在触摸它?

我可以检查每次发生新的touchesBegan或touchesEnded时。

最佳答案

首先在您的子类上启用多点触控

self.isMultipleTouchEnabled = true

然后在触摸事件函数中,您可以从 UIView 类中获取所有带有事件的触摸。

let touchCount = event?.touches(for: self)?.count

关于ios - 获取触摸 UIView 的手指数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43474362/

相关文章:

iphone - objective-c 中的多维数组

iphone - Cocos2d的touch locationInView、covertToGL、convertToNodeSpace有什么区别?

ios - 如何展示真实的 Admob 广告而不是测试广告?

objective-c - 标签栏应用问题

ios - Result.Framework 未在 TrueTime 中找到?

iphone - 使用 iOS 7 beta 对 iOS 应用程序进行 Beta 测试

iphone - 在 NSMutableArray 中存储字体名称

string - IOS9 - 无法使用类型为 'count' 的参数列表调用 '(String)'

ios - Xcode Storyboard 显示为空

ios - 无法调用参数列表类型