【问题标题】:How are targets getting triggered in UIButton?UIButton 中的目标是如何被触发的?
【发布时间】:2014-03-03 20:40:38
【问题描述】:

我在视图中有一个 UIButton,在该按钮上我有一个 UILabel (TTTAttributedLabel)。 (https://github.com/mattt/TTTAttributedLabel) (虽然,这不是与 TTTAttributedLabel 自定义类相关的直接问题)

这个 UILabel 执行一些带有触摸的动作,如果不喜欢触摸坐标,它会将它们转发给 super 并在相应的方法中使用以下内容:

    [super touchesBegan:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
    [super touchesCancelled:touches withEvent:event];

我看到在这种情况下按下了下面的按钮。我还看到UIButton 中的所有这些方法都被完美地调用了。我还记录 UITouch 对象以查看它们是否匹配。

问题是,设置为UIButton 的操作没有被调用(即使是UIControlEventTouchDown)。是什么触发了这些动作?我认为与 touchesEnded 匹配的 touchesBegan 应该调用为该按钮添加的目标操作的选择器。

【问题讨论】:

    标签: ios objective-c uibutton uitouch


    【解决方案1】:

    触摸触发控制事件,我会说您的按钮没有收到触摸只是因为按钮上有一个标签。

    有两种方法可以解决这个问题。首先,可能首选的方法是使用 UIButtonatributedTitle 属性,而不是在不需要的地方混搭一个附加标签: https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/attributedTitleForState:

    另一种方法是深入研究UIControl,可能将其子类化,然后在更精细的范围内处理触摸事件,尽管这比上面的解决方案复杂得多: https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/reference/reference.html

    【讨论】:

    • 按钮接收触摸,因为我将其子类化并在其中查看触摸事件的 NSLog 输出。不知何故,转发的触摸可能不匹配......但它们似乎匹配,因为对象 id 和坐标在日志中是相同的。 (我不能直接使用按钮的标题属性,因为标签中有可点击的单词。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2023-04-03
    • 1970-01-01
    • 2014-09-05
    • 2016-07-31
    • 1970-01-01
    相关资源
    最近更新 更多