ios - UIPinchGestureRecognizer分别在水平和垂直方向缩放

标签 ios scale pinch uipinchgesturerecognizer pinchzoom

当使用 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/

相关文章:

android - 如何在 Fresco Library 中实现缩放?

ios - ARMv7 的 OSX 交叉编译库

iphone - 动态创建 uibarbutton 不起作用

ios - addSkipBackupAttributeToItemAtURL -> NSString 参数?

javascript - 垂直反转 Raphael SVG 形状不起作用

jquery - jQuery —调整多个对象的大小,链接宽度和高度比例

r - 组间分布的可视化比较 : How is scale modified for "asymmetric beanplots"?

Android:缩放 ImageView 及其 onDraw() 项目

ios:捏手势只发生一次

ios - 将表的行数设置为特定实体