同时捏,拖和平移

时间:2016-12-02 14:26:20

标签: ios swift uigesturerecognizer uipinchgesturerecognizer

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

enter image description here

标签可拖动,可平移和可捏合。但是我一次只能做一个手势。例如,我想在我捏它时拖动标签,就像在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
    }
}

0 个答案:

没有答案