如何正确地继承UIControl? [英] How to correctly subclass UIControl?
问题描述
我不想要 UIButton
或类似的东西。我想直接子化 UIControl
并制作我自己的非常特殊的控件。
I don't want UIButton
or anything like that. I want to subclass UIControl
directly and make my own, very special control.
但由于某种原因,没有我覆盖的任何方法都会被调用。目标动作的东西起作用,目标接收适当的动作消息。但是,在我的 UIControl
子类中,我必须捕获触摸坐标,这样做的唯一方法似乎是覆盖这些人:
But for some reason, none of any methods I override get ever called. The target-action stuff works, and the targets receive appropriate action messages. However, inside my UIControl
subclass I have to catch touch coordinates, and the only way to do so seems to be overriding these guys:
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"begin touch track");
return YES;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"continue touch track");
return YES;
}
他们从未被调用过,即使 UIControl
使用来自 UIView
, initWithFrame:
的指定初始化程序进行实例化。
They get never called, even though the UIControl
is instantiated with the designates initializer from UIView
, initWithFrame:
.
我能找到的所有例子总是使用 UIButton
或 UISlider
作为子类化的基础,但我想更接近 UIControl
,因为这是我想要的来源:快速和未延迟的触摸坐标。
All examples I can find always use a UIButton
or UISlider
as base for subclassing, but I want to go closer to UIControl
since that's the source for what I want: Fast and undelayed Touch coordinates.
推荐答案
我知道这个问题很古老,但我遇到了同样的问题,我想我应该给出2美分。
I know this question is ancient, but I had the same problem and I thought I should give my 2 cents.
如果您的控件有任何子视图, beginTrackingWithTouch
, touchesBegan
等可能无法调用,因为这些子视图正在吞下触摸事件。
If your control has any subviews at all, beginTrackingWithTouch
, touchesBegan
, etc might not get called because those subviews are swallowing the touch events.
如果您不希望这些子视图处理触摸,您可以将 userInteractionEnabled
设置为否
,因此子视图只是传递事件通过。然后你可以覆盖 touchesBegan / touchesEnded
并管理那里的所有动作。
If you don't want those subviews to handle touches, you can set userInteractionEnabled
to NO
, so the subviews simply passes the event through. Then you can override touchesBegan/touchesEnded
and manage all your touches there.
希望这有帮助。
这篇关于如何正确地继承UIControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!