gpt4 book ai didi

ios - 点击 super View 中嵌入的自定义 UIView 中未收到的手势识别器

转载 作者:行者123 更新时间:2023-11-28 10:19:27 24 4
gpt4 key购买 nike

我正在尝试创建一个名为 MyScrollView 的自定义 UIView/Scrollview,其中包含一些标签 (UILabel),并且这些标签接收点击手势/事件以响应用户的选择 See the below screen shot for the custom UIView .

为了使点击事件在 UILabel 上起作用,我确保它们都具有 userIteractionEnabled = true 并且我创建了一个委托(delegate),如下所示:

protocol MyScrollViewDelegate {
func labelClicked(recognizer: UITapGestureRecognizer)
}

我创建的 ScrollViewController 中使用了自定义 UIView,这个 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 作为 subview Controller 添加到父 View Controller ,并定位到父 View 的顶部:

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)
}

应用程序可以在 View 中加载所有内容,但点击手势/事件不会传递到自定义 MyScrollView 中的标签。为此,我做了一些谷歌搜索并阅读了 Event Delivery: Responder Chain在 Apple Developer 网站上也进行了 HitTest 。下面的hitTest函数可以在MyScrollView中触发:

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
print("hit test started, point: \(point), event: \(event)")
return self
}

我对 hitTest 的观察是 touchesBegan()touchesEnded() 方法仅在 View 中触发hitTest 函数存在时。没有 hitTest,这两个函数都不会被点击调用。

但没有运气让 UILabel 响应点击手势。所以我在这里联系 SO 专家。感谢您的帮助!

最佳答案

我想我找到了 UILabel 的原因经过一番挣扎后没有回应:.addGestureRecognizer()标签的方法在 init() 中运行我的习惯方法UIView组件,这是错误的,因为 View /标签 可能尚未呈现。相反,我将该代码移至生命周期方法 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 委托(delegate)方法,也不需要创建容器 View 或 ScrollView userInteractionEnabled .更重要的是,当将自定义 UIView 嵌入到 super View 时,我配置了它的大小并设置了 clipsToBounds = true .

我想我应该阅读更多 UIView Apple Developer 网站上的文档。希望这会在将来帮助像我这样的人!感谢大家!

关于ios - 点击 super View 中嵌入的自定义 UIView 中未收到的手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37007655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com