UIPanGestureRecognizer并未启动。斯威夫特3

时间:2017-03-21 23:37:59

标签: ios swift swift3 uigesturerecognizer

我的视图层次结构如下:

  • 查看

    • 查看用户的Img + Emoji(A)
    • 使用UI元素查看。 (B)

在viewDidLoad中,我为emojis创建UIImageViews,将它们添加到A并添加手势识别器(pan,pinch,rotate):

 let emojiView = UIImageView(image: emoji)
            emojiView.tag = n
            emojiView.frame = CGRect(x: 153, y: 299, width: 70, height: 70)
            emojiView.isUserInteractionEnabled = true

            let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
            pan.delegate = self
            emojiView.addGestureRecognizer(pan)

            let pinch = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch(recognizer:)))
            pinch.delegate = self
            emojiView.addGestureRecognizer(pinch)

            let rotate = UIRotationGestureRecognizer(target: self, action: #selector(self.handleRotate(recognizer:)))
            rotate.delegate = self
            emojiView.addGestureRecognizer(rotate)

稍后,我宣布IBActions:

@IBAction func handlePan(recognizer: UIPanGestureRecognizer) {
    let translation = recognizer.translation(in: self.viewForImgAndEmoji)
    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: self.viewForImgAndEmoji)
}

@IBAction func handlePinch(recognizer: UIPinchGestureRecognizer) {

    let pinchPoint = recognizer.location(in: viewForImgAndEmoji)
    let ourEmojiView = viewForImgAndEmoji.hitTest(pinchPoint, with: nil)

    ourEmojiView!.transform = ourEmojiView!.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
    recognizer.scale = 1

}

@IBAction func handleRotate(recognizer: UIRotationGestureRecognizer){

    let rotatePoint = recognizer.location(in: viewForImgAndEmoji)
    let ourEmojiView = viewForImgAndEmoji.hitTest(rotatePoint, with: nil)

    ourEmojiView!.transform = ourEmojiView!.transform.rotated(by: recognizer.rotation)
    recognizer.rotation = 0
}

问题:pan功能没有启动。旋转和捏合工作。但是平移并不是。 (由于它有按钮,因此无法禁止用户交互以覆盖视图)

2 个答案:

答案 0 :(得分:1)

您可能需要实现以下委托方法:

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

答案 1 :(得分:1)

好的,我看了一下代码,看到了两个可能的问题:

1:您还可以在故事板中设置捏合,平移和旋转手势识别器。这些可能是多余的。只是提到这一点。这实际上不是您的平移手势识别器无法正常工作的问题。

2:控件的 int p; int j; printf("Who is Palpatine?\n"); scanf("%s", &j); if (j = "Senate") { printf("Your number sir?\n"); scanf("%d", &p); printf("CT %d, the time has come. Execute Order Sixty-Six.\n", p); } else { printf("Incorrect\n"); } return 0; } 涵盖您的表情符号和图片视图,并将接管触控。您不需要UI控件的全屏视图,因为您似乎对视图本身没有任何作用。相反,您只需将按钮放在主屏幕上UIView上方的正确位置即可。这样,任何屏幕上的触摸都会达到viewForImgAndEmoji,除非您有按钮的位置。

我以这种方式修改了故事板,我可以在那时让平移手势很好。如果你需要我澄清任何事情,请告诉我,因为我不确定我对这个问题是否足够清楚:)

注意:修复上述内容时遇到的一个问题是,您会发现并非所有触摸都会注册,因为手势识别器已贴在每个贴纸上,贴纸本身也可能小的,以检测像捏手势的东西。您可能最好添加viewForImgAndEmoji作为手势处理程序,并根据在尝试捏,旋转或平移之前通过点击选择表情符号来激活每个贴纸/表情符号作为手势的接收者。只是一个建议:)