ios - 如何告诉我的应用忽略特定触摸?
<p><p>我的 iPad 应用需要能够完全消除特定的触摸,即来自手指或触控笔的触摸,而<strong>不是</strong>来自手掌的触摸。该 View 启用了多点触控,因此我可以分别分析每个触控。</p>
<p>我目前可以使用 <code>majorRadius</code> 属性区分这些触摸,但我不确定如何消除较大的触摸,即那些大于我决定的阈值的触摸。</p>
<pre><code>let touchThreshold : CGFloat = 21.0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if touch.majorRadius > touchThreshold {
//dismiss the touch(???)
} else {
//draw touch on screen
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if touch.majorRadius > touchThreshold {
//dismiss the touch(???)
} else {
//draw touch on screen
}
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果您只想检测某些类型的触摸,这里有一个可能的解决方案:</p>
<p><em>选项 1</em></p>
<pre><code>override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touches.forEach { touch in
switch touch.type {
case .direct, .pencil:
handleTouch(for: touch.majorRadius)
case .indirect:
// don't do anything
return
}
}
}
func handleTouch(for size: CGFloat) {
switch size {
case _ where size > touchThreshold:
print("do something")
default:
print("do something else")
}
}
</code></pre>
<p><em>选项 2</em></p>
<pre><code>override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touches.filter{($0.type == .direct || $0.type == .pencil || $0.type == .stylus) && $0.majorRadius > touchThreshold}.forEach { touch in
print("do something")
}
}
</code></pre>
<p>您可以阅读更多触摸类型<a href="https://developer.apple.com/documentation/uikit/uitouch/touchtype#" rel="noreferrer noopener nofollow">here</a> .</p></p>
<p style="font-size: 20px;">关于ios - 如何告诉我的应用忽略特定触摸?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/54358278/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/54358278/
</a>
</p>
页:
[1]