ios GestureRecognizer 没有响应点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26028455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:47:22  来源:igfitidea点击:

GestureRecognizer not responding to tap

iosswiftuigesturerecognizer

提问by Didia

After initialisation of by subclass of UIImageViewI have the following line of code:

按子类初始化后,UIImageView我有以下代码行:

self.userInteractionEnabled = true
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))

I created the necessary associated function :

我创建了必要的关联函数:

func handleTap(gestureRecognizer: UITapGestureRecognizer) {
    print("In handler")
}

On tapping on the view in question, "In handler was never printed to the console". I then removed the handler function to see if the compiler would complain about the missing function. It didn't.

在点击有问题的视图时,“In handler 从未打印到控制台”。然后我删除了处理函数,看看编译器是否会抱怨缺少的函数。它没有。

I'm positively stumped. I'd truly appreciate any light people can shed on this.

我很难过。我真的很感激人们可以对此有所了解。

Update: My class is actually a UIImageViewas opposed to UIView

更新:我的班级实际上是一个UIImageView而不是UIView

采纳答案by Didia

I discovered the answer after carefully combing through my code.

在仔细梳理我的代码后,我找到了答案。

One of the parent views was created without supplying a frame:

创建父视图之一时未提供 frame:

While it's a noobish enough error to warrant deletion of this questions, odds are someone else will also have the same issue in the future...

虽然这是一个足够愚蠢的错误来保证删除这些问题,但很可能其他人将来也会遇到同样的问题......

回答by Scooter

I was using UITapGestureRecognizerthat I placed on a UILabelusing Storyboard.

我正在使用UITapGestureRecognizer我放置在UILabel使用 Storyboard上的那个。

To get this to work I also had to place a checkmark in the block labeled: "User Interaction Enabled" in the UILabelAttributes Inspector in the Storyboard.

为了让它起作用,我还必须UILabel在故事板的属性检查器中标记为“启用用户交互”的块中放置一个复选标记。

enter image description here

在此处输入图片说明

回答by Suresh Kumar Durairaj

Try this

尝试这个

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.view.userInteractionEnabled = true
    var tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
    self.view.addGestureRecognizer(tapGesture)
}

func handleTap(sender : UIView) {
    println("Tap Gesture recognized")
}

回答by bradkratky

In addition to the other answers, this can be caused by adding the gesture recognizer to multiple views. Gesture recognizers are for single views only.

除了其他答案之外,这可能是由于将手势识别器添加到多个视图而引起的。手势识别器仅适用于单一视图。

Reference: https://stackoverflow.com/a/5567684/6543020

参考:https: //stackoverflow.com/a/5567684/6543020

回答by Vitaly

Most likely you add UIGestureRecognizerin wrong place. Here is working sample with UIViewfrom storyboard. If you create your UIViewdynamically then you should put this initialization in the correct constructor.

很可能你UIGestureRecognizer在错误的地方添加了。这是UIViewfrom 的工作示例storyboard。如果您UIView动态创建,那么您应该将此初始化放在正确的构造函数中。

class TestView: UIView
{
    override func awakeFromNib()
    {
        self.userInteractionEnabled = true
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
    }

    func handleTap(gestureRecognizer: UITapGestureRecognizer)
    {
        println("Here")
    }
}

回答by mattdedek

I ran into this problem with programmatic views.

我在程序化视图中遇到了这个问题。

My UIView with the gesture recognizer had .isUserInteractionEnabled = true, but it did not respond to taps until I set .isUserInteractionEnabled = true for its parent views as well.

我的带有手势识别器的 UIView 有 .isUserInteractionEnabled = true,但在我为其父视图设置 .isUserInteractionEnabled = true 之前,它没有响应点击。

回答by Tarek

This Code works for me with XCode 7.0.1

此代码适用于 XCode 7.0.1

import UIKit

class ImageView: UIImageView {

    init(frame: CGRect, sender: Bool, myImage: UIImage) {
        super.init(frame: frame)
        self.image = myImage
        initBorderStyle(sender)

        // enable user interaction on image.
        self.userInteractionEnabled = true
        let gesture = UITapGestureRecognizer(target: self, action: "previewImage:")
        addGestureRecognizer(gesture)

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func previewImage(myGesture: UITapGestureRecognizer? = nil) {
        print("i'm clicked")
    }

    private func initBorderStyle(sender: Bool) {
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 8
        self.layer.borderWidth = 0.5
        self.layer.borderColor = getBorderColor(sender)
        self.backgroundColor = getColor(sender)
    }


    func getBorderColor(sender: Bool) -> CGColor {
        var result: CGColor
        if sender {
            result = UIColor(red: 0.374, green: 0.78125, blue: 0.0234375, alpha: 0.5).CGColor
        } else {
            result = UIColor(red: 0.3125, green: 0.6015625, blue: 0.828125, alpha: 0.5).CGColor
        }
        return result
    }
}

回答by Mayank Wadhwa

I found the solution to this problem after lot of trial and error. So there are two solution two this

经过多次反复试验,我找到了解决此问题的方法。所以有两个解决方案二这个

1.Either add the GestureRecognizerin viewDidLoad() and turn userInteractionEnabled= true

1.要么添加GestureRecognizerin viewDidLoad() 并转 userInteractionEnabled=true

2.If using computed property use lazy varinstead of letto the property.

2.如果使用计算属性,请使用lazy var而不是letto 属性。

lazy var profileImageView: UIImageView = {
    let iv = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    iv.image = #imageLiteral(resourceName: "gameofthrones_splash")
    iv.contentMode = .scaleAspectFill
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
    iv.isUserInteractionEnabled = true
    return iv
}()

回答by George_E

It turned out that my gesture didn't work because it was in a normal class, and not a subclass of UIViewor anything.

结果我的手势不起作用,因为它在一个普通的类中,而不是一个子类UIView或任何东西。

I solved the issue by creating a subclass of UIViewthat I had an instance of in this normal class and placed the gesture logic in that.

我通过UIView在这个普通类中创建一个我有一个实例的子类来解决这个问题,并将手势逻辑放在其中。

回答by Jeff

In addition the above (userInteractionEnabled, clipsToBounds, etc.), if you are working with a view in a child view controller be sure that you have added it as a child with myParentViewController.addChild(myChildViewController)—?we've run into a couple of situations now where visible views were not firing recognizing gestures because their view controllers hadn't been added, and presumably the VCs themselves were not being retained.

除了上面的(userInteractionEnabled,,clipsToBounds等等),如果你在子视图控制器中使用一个视图,请确保你已经将它作为子视图添加myParentViewController.addChild(myChildViewController)-?我们现在遇到了几种情况,其中可见视图是没有触发识别手势,因为他们的视图控制器没有被添加,并且大概 VC 本身没有被保留。