子视图手势识别器未调用 [英] Subview Gesture Recognizer not being called

查看:57
本文介绍了子视图手势识别器未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使自定义UIView.UI中的UITapGestureRecognizer正常工作时遇到问题.我已经创建了一个视图:CategoryViewButton,它在init中添加了一个UITapGestureRecognizer:

I am having a problem getting the UITapGestureRecognizer in my custom UIView.to work properly. I Have created a view: CategoryViewButton which adds a UITapGestureRecognizer in the init:

class CategoryViewButton: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))
    self.addGestureRecognizer(tapGesture)
 }

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
 }

func handleTap() {
    print("Hello again")
 }
}

直接在View Controller中添加时,此手势识别器可以正常工作.但是,当我将CategoryViewButton添加为另一个自定义视图的子视图时,不会识别手势识别器方法.我的子视图:

This gesture recognizer works without issue when added directly in a View Controller. However, when I add a CategoryViewButton as a subview of another custom view, the gesture recognizer method does not get called. My subview:

class CategoryView: UIView, CategoryButtonDelegate {

var button : CategoryViewButton?

override init(frame: CGRect) {
    super.init(frame: frame)
    button = CategoryViewButton(frame: CGRect(x: 10, y: 0, width: 40, height: 25))
    self.addSubview(button!)
    self.bringSubview(toFront: button!)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
 }
}

当我在视图控制器中创建CategoryView时,未调用handleTap()函数.我想念什么?

When I create a CategoryView in a View Controller, the handleTap() function is not being called. What am I missing?

推荐答案

对于任何好奇的人,问题在于带有手势识别器的子视图不在超级视图的框架之内.这意味着即使绘制了视图,也没有检测到手势

For anyone curious, the issue was that the subview with gesture recognizer was outside the frame of the superview. This means even though the view was being drawn, the gestures were not detected

这篇关于子视图手势识别器未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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