我试图创建一个名为MyScrollView的自定义UIView/Scrollview,它包含一些标签(UILabel),这些标签接收tap手势/事件,以便响应用户的选择ios - 在 super  View 中嵌入的自定义UIView中未收到Tap Gesture Recognizer-LMLPHP
为了使tap事件在UILabels上工作,我确保它们都有userIteractionEnabled = true并且我创建了一个委托,如下所示:

protocol MyScrollViewDelegate {
    func labelClicked(recognizer: UITapGestureRecognizer)
}

自定义UIView正在我创建的ScrollViewController中使用,这个ScrollViewController还实现了delegate方法:
import UIKit
import Neon
class ScrollViewController: UIViewController, MyScrollViewDelegate {

var curQuestion: IPQuestion?
var type: QuestionViewType?
var lastClickedLabelTag: Int = 0 //

init(type: QuestionViewType, question: IPQuestion) {
    super.init(nibName: nil, bundle: nil)
    self.curQuestion = question
    self.type = type
}

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

override func viewDidLoad() {
    super.viewDidLoad()
    self.automaticallyAdjustsScrollViewInsets = false
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func loadView() {
    view = MyScrollView(delegate: self, q: curQuestion!)
    view.userInteractionEnabled = true
}
}

// implementations for MyScrollViewDelegate
extension ScrollViewController {

func labelTitleArray() -> [String]? {
    print("labelTitleArray called in implemented delegate")

    return ["Comments", "Answers"]
}

func labelClicked(recognizer: UITapGestureRecognizer) {
    print("labelClicked called in implemented delegate")
    let controller = parentViewController as? ParentViewController
    controller?.labelClicked(recognizer)
    lastClickedLabelTag = recognizer.view!.tag
    }
}

// MARK: - handle parent's ViewController event
extension QuestionDetailViewController {
    func updateActiveLabelsColor(index: Int) {
        print("updating active labels color: \(index)")
        if let view = view as? MyScrollView {
            for label in (view.titleScroll.subviews[0].subviews as? [UILabel])! {
                if label.tag == index {
                    label.transform = CGAffineTransformMakeScale(1.1,1.1)
                    label.textColor = UIColor.purpleColor()
                }
                else {
                    label.transform = CGAffineTransformMakeScale(1,1)
                    label.textColor = UIColor.blackColor()
                }
            }
        }
    }
}

上面的ScrollViewController作为子视图控制器添加到父视图控制器,并定位到父视图的顶部:
override func viewDidLoad() {
        super.viewDidLoad()
        self.automaticallyAdjustsScrollViewInsets = false
        self.view.backgroundColor = UIColor.whiteColor()
        addChildViewController(scrollViewController) // added as a child view controller here
        view.addSubview(scrollViewController.view) // here .view is MyScrollView
        scrollViewController.view.userInteractionEnabled = true
        scrollViewController.view.anchorToEdge(.Top, padding: 0, width: view.frame.size.width, height: 100)
    }

应用程序可以加载视图中的所有内容,但点击手势/事件不会传递给自定义MyScrollView中的标签。为此,我做了一些谷歌搜索,阅读了苹果开发者网站上的Event Delivery: Responder Chain,并做了一个点击测试。下面的hitTest功能可以在MyScrollView中触发:
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        print("hit test started, point: \(point), event: \(event)")
        return self
    }

我对hitTest的观察是,只有当touchesBegan()函数存在时,touchesEnded()hitTest方法才会在视图中触发。如果没有hitTest,两个函数都不能用taps调用。
但是让UILabel对轻敲手势做出响应是不可能的。所以我在这里联系专家。谢谢你的帮助!

最佳答案

我想我已经找到了为什么UILabel在经过多次挣扎后没有响应轻敲的原因:标签的.addGestureRecognizer()方法是在我的自定义init()组件的UIView方法中运行的,这是错误的,因为视图/标签可能还没有呈现。相反,我将代码移到lifecycle方法layoutSubviews(),一切都开始正常工作:

var lastLabel: UILabel? = nil

        for i in 0..<scrollTitleArr.count {
            let label = UILabel()
            label.text = scrollTitleArr[i] ?? "nothing"
            print("label: \(label.text)")
            label.font = UIFont(name: "System", size: 15)
            label.textColor = (i == 0) ? MaterialColor.grey.lighten2 : MaterialColor.grey.darken2
            label.transform = (i == 0) ? CGAffineTransformMakeScale(1.1, 1.1) : CGAffineTransformMakeScale(0.9, 0.9)
            label.sizeToFit()
            label.tag = i // for tracking the label by tag number
            label.userInteractionEnabled = true
            label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.labelClicked(_:))))

            titleContainer.addSubview(label)

            if lastLabel == nil {
                label.anchorInCorner(.TopLeft, xPad: 0, yPad: 0, width: 85, height: 40)
      //         label.anchorToEdge(.Left, padding: 2, width: 85, height: 40)
            } else {
                label.align(.ToTheRightMatchingTop, relativeTo: lastLabel!, padding: labelHorizontalGap, width: 85, height: 40)
            }

            lastLabel = label

        }

此外,我不需要实现任何UIGestureRecognizer委托方法,也不需要创建容器视图或滚动视图userInteractionEnabled。更重要的是,在将自定义UIView嵌入到superview时,我配置了它的大小并设置了clipsToBounds = true
我想我应该在Apple开发者网站上阅读更多的UIView文档。希望这能帮助像我这样的人在未来!谢谢大家!

关于ios - 在 super View 中嵌入的自定义UIView中未收到Tap Gesture Recognizer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37007655/

10-09 22:13
查看更多