当使用 UIPinchGestureRecognizer 时,分别检测/读取水平和垂直方向的捏合比例的最佳方法是什么?我看到了这个帖子
UIPinchGestureRecognizer Scale view in different x and y directions
但我注意到,为了这样一个看似例行的任务,有太多人来回奔波,我不确定这是最好的答案/方式。
如果不为此目的完全使用 UIPinchGestureRecognizer 就是答案,那么在两个不同方向上检测捏合比例的最佳方法是什么?
最佳答案
基本上就是这样,
func _mode(_ sender: UIPinchGestureRecognizer)->String {
// very important:
if sender.numberOfTouches < 2 {
print("avoided an obscure crash!!")
return ""
}
let A = sender.location(ofTouch: 0, in: self.view)
let B = sender.location(ofTouch: 1, in: self.view)
let xD = fabs( A.x - B.x )
let yD = fabs( A.y - B.y )
if (xD == 0) { return "V" }
if (yD == 0) { return "H" }
let ratio = xD / yD
// print(ratio)
if (ratio > 2) { return "H" }
if (ratio < 0.5) { return "V" }
return "D"
}
该函数将为您返回 H、V、D .. 水平、垂直、对角线。
你会像这样使用它......
func yourSelector(_ sender: UIPinchGestureRecognizer) {
// your usual code such as ..
// if sender.state == .ended { return } .. etc
let mode = _mode(sender)
print("the mode is \(mode) !!!")
// in this example, we only care about horizontal pinches...
if mode != "H" { return }
let vel = sender.velocity
if vel < 0 {
print("you're squeezing the screen!")
}
}
关于ios - UIPinchGestureRecognizer分别在水平和垂直方向缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12354959/