【问题标题】:Pinch, drag and pan at the same time同时捏合、拖动和平移
【发布时间】:2016-12-02 14:26:20
【问题描述】:

我在UIImageView 上有一个标签,如下所示。

标签可拖动、平移和捏合。但是我一次只能做一个手势。例如,我想在捏标签时拖动标签,就像在 Snapchat 和 Whatsapp 中的图像上的文本中一样。我的功能如下。当我搜索时,我认为我应该创建一个自定义手势识别器,但我不知道如何。有什么方法可以在不创建自定义识别器的情况下做到这一点?

在执行此操作时,我从这篇文章中获得了帮助: Snapchat-like text on image

 func handlePan(recognizer: UIPanGestureRecognizer) {
    var translation = recognizer.translation(in: allview)

    translation.x = max(translation.x, imageview.frame.minX - mylabel.frame.minX)
    translation.x = min(translation.x, imageview.frame.maxX - mylabel.frame.maxX)

    translation.y = max(translation.y, imageview.frame.minY - mylabel.frame.minY)
    translation.y = min(translation.y, imageview.frame.maxY - mylabel.frame.maxY)

    if let view = recognizer.view {
        view.center = CGPoint(x:view.center.x + translation.x,
                              y:view.center.y + translation.y)
    }
    recognizer.setTranslation(CGPoint.zero , in: view)
}
func handlePinch(recognizer: UIPinchGestureRecognizer) {
    if let view = recognizer.view as? UILabel {
        let pinchScale: CGFloat = recognizer.scale
        view.transform = view.transform.scaledBy(x: pinchScale, y: pinchScale)
        recognizer.scale = 1.0
    }
}
func handleRotate(recognizer: UIRotationGestureRecognizer) {
    if let view = recognizer.view as? UILabel {
        let rotation: CGFloat = recognizer.rotation
        view.transform = view.transform.rotated(by: rotation)
        recognizer.rotation = 0.0
    }
}

【问题讨论】:

  • 我敢打赌,尝试一次做多个手势是糟糕的设计。如果您考虑普通用户的期望,这听起来令人困惑。
  • 我想像在 snapchat 或 whatsapp(图片上的文字)中那样做。

标签: ios swift uigesturerecognizer uipinchgesturerecognizer


【解决方案1】:

我通过将“UIGestureRecognizerDelegate”添加到我的 ViewController 来解决。这允许同时使用手势。我确信创建自定义手势会更好,但这也可以。在 viewDidLoad 函数上添加这三行代码

pinchRecognizer.delegate = self
panRecognizer.delegate = self
rotateRecognizer.delegate = self

另外不要忘记为委托添加functin;

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-10
    • 2023-03-27
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2021-11-30
    相关资源
    最近更新 更多