ios - 未调用 subview 手势识别器

标签 ios iphone swift uiview uigesturerecognizer

我在让自定义 UIView 中的 UITapGestureRecognizer 正常工作时遇到问题。我创建了一个 View :CategoryViewButton,它在 init 中添加了一个 UITapGestureRecognizer:

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 添加为另一个自定义 View 的 subview 时,不会调用手势识别器方法。我的 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")
 }
}

当我在 View Controller 中创建类别 View 时,没有调用 handleTap() 函数。我错过了什么?

最佳答案

对于任何好奇的人来说,问题是带有手势识别器的 subview 在父 View 的框架之外。这意味着即使正在绘制 View ,也未检测到手势

关于ios - 未调用 subview 手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38043832/

相关文章:

ios - 在 SwiftUI 中动态创建列表部分

ios - Swift 中的 ABAddressBook iOS 8 内存泄漏

ios - 为什么 Xcode 5 一直给我这些错误?

ios - 跨多个线程的 println() 导致混合消息

iphone - 如何在 uitouch 上删除部分图像以进行 ios 编程?

ios - 如何在 iOS 中发送消息通知?

swift - 生成特定数量的 SCNPlane [Swift、ARkit]

algorithm - iOS:优化位图模糊算法

ios - 在 Objective C 中使用 NSPredicate 过滤自定义对象数组

iphone - 如何使用最新的 Xcode 下载来针对旧版 iOS 进行构建?