UIPanGestureRecognizer 没有启动.斯威夫特 3 [英] UIPanGestureRecognizer isn't firing up. Swift 3

查看:24
本文介绍了UIPanGestureRecognizer 没有启动.斯威夫特 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图层次如下:

  • 查看

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

在 viewDidLoad 中,我为表情符号创建 UIImageViews,将它们添加到 A 并添加手势识别器(平移、捏合、旋转):

In viewDidLoad, I create UIImageViews for emojis, add them to the A and add gesture recognizers (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:

Later, I declare 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 func 没有启动.旋转和捏合作品.但平移不是.(无法禁用叠加视图的用户交互,因为它有按钮)

Problem: the pan func isn't firing up. Rotate and pinch works. But panning isn't. (Can't disable user interaction for overlaying view since it has buttons)

推荐答案

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

OK, I took a look at the code and I see two possible issues:

1:您还可以在情节提要中设置捏合、平移和旋转手势识别器.这些可能是多余的.就是提一下.这确实不是您的平移手势识别器无法正常工作的问题.

1: You have pinch, pan, and rotate gesture recognizers set in the storyboard as well. These might be superfluous. Just mentioning that. This really isn't the issue your pan gesture recognizer isn't working though.

2:控件的 UIView 覆盖你的表情符号和图像视图,并将接管触摸.您不需要 UI 控件的全屏视图,因为您似乎没有对视图本身做任何事情.相反,您可以简单地将按钮放在主屏幕上 viewForImgAndEmoji 上方的正确位置.这样,屏幕上的任何触摸都会到达 viewForImgAndEmoji,除了您有按钮的地方.

2: The UIView for controls covers your emoji and image view and will take over touches. You don't need a full-screen view for the UI controls since you don't seem to do anything with the view itself. Instead, you can simply put the button at the right position on the main screen over the viewForImgAndEmoji. This way, any on screen touches reach viewForImgAndEmoji except at the spots where you have the buttons.

我以这种方式修改了情节提要,那时我可以很好地调整平移手势.如果您需要我澄清任何事情,请告诉我,因为我不确定我对这个问题是否足够清楚:)

I modified the storyboard that way and I could get the pan gestures fine at that point. Do let me know if you need me to clarify anything since I am not sure if I was clear enough about the issue :)

注意:解决上述问题时您会遇到的一个问题是,您会发现并非所有触摸都注册,因为手势识别器附加到每个贴纸上,而贴纸本身可能太小以检测诸如捏合手势之类的东西.您最好添加 viewForImgAndEmoji 作为手势处理程序,并在尝试捏合、旋转或平移之前通过轻按选择表情符号来激活每个贴纸/表情符号作为手势的接收者.只是一个建议:)

Note: One issue that you will run into when you fix the above is that you will find that not all touches register because the gesture recognizer is attached to each sticker and the sticker itself might be too small to detect something like a pinch gesture. You might be better off adding viewForImgAndEmoji as the gesture handler and activating each sticker/emoji as the recipient of the the gesture based on selecting the emoji by a tap before you try to pinch, rotate, or pan. Just a suggestion :)

这篇关于UIPanGestureRecognizer 没有启动.斯威夫特 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆