问题描述
我目前正在研究自定义 UIControl 子类.要跟踪触摸,我使用以下方法:
I'm currently working on a custom UIControl Subclass. To track the touches I use the following Method:
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(@"Start");
CGPoint location = [touch locationInView:self];
if ([self touchIsInside:location] == YES) {
//Touch Down
[self sendActionsForControlEvents:UIControlEventTouchDown];
return YES;
}
else {
return NO;
}
}
这按预期工作,@"Start" 只记录一次.下一步是我使用 UIControlEventTouchDown 添加一个 Target 和一个 Selector.
This works as expected and @"Start" is loged exactely once. The next step is that I add a Target and a Selector with UIControlEventTouchDown.
[markItem addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
这也有效,并且调用了 action: 方法.但那是我的问题.该动作被调用两次.我究竟做错了什么?我只使用 [self sendActionsForControlEvents:UIControlEventTouchDown];
一次,目标操作被调用两次.我的代码有什么问题?
This works also and the action: method is called. But that's my problem. The action is called twice. What am I doing wrong? I just use [self sendActionsForControlEvents:UIControlEventTouchDown];
once and the target action is called twice. What's wrong with my code?
桑德罗·迈耶
推荐答案
第一次调用 action 方法会在您调用后由事件调度程序自动发生:
The first call to the action method happens automatically by the event dispatcher once you've called:
[markItem addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
注册处理程序.
所以当你打电话时:
//Touch Down
[self sendActionsForControlEvents:UIControlEventTouchDown];
您正在生成对您的处理程序(以及任何其他连接的)的第二个调用.
you are generating the second call to your handler (and any others that are hooked up).
所以看起来你不需要动作处理程序和 beginTracking - 使用其中之一.
So it seems like you don't need both the action handler and the beginTracking - use one or the other.
更新:
根据您的评论和进一步思考:由于您是 UIControl 的子类,我认为您可能不想为自己注册事件处理程序.
Given your comment and further thought: since you are a subclass of UIControl, I think you probably don't want to be registering for event handlers for yourself.
相反,您应该专门使用:
Instead you should exclusively use:
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)cancelTrackingWithEvent:(UIEvent *)event; // event may be nil if cancelled for non-event reasons, e.g. removed from window
也是 tracking
实例变量.
所以我认为您不应该发布活动或收听活动.此外,如果它不在您的视野中,是否真的有可能获得 beginTrackingWithTouch 事件?似乎不会.所以我认为你不需要测试来看看它是否在你的视野中.
So I think you should not be posting events or listening to events. Further, is it actually possible to get a beginTrackingWithTouch event if it's not in your view? Doesn't seem like it would be. So I don't think you need the testing to see if it's in your view.
所以我认为有必要退后一步思考您正在尝试做什么并重新阅读 UIControl 文档.具体:
So I think it might be worth stepping back and thinking about what you are trying to do and re-reading UIControl documentation. Specifically:
子类化注释你可能想扩展一个 UIControl 子类两个原因之一:
观察或修改动作消息到目标的分派特定事件为此,覆盖sendAction:to:forEvent:
,评估传入的选择器、目标对象或 UIControlEvents
位掩码,然后按要求进行.
To observe or modify the dispatch of action messages to targets for particular events To do this, override sendAction:to:forEvent:
, evaluate the passed-in selector, target object, or UIControlEvents
bit mask, and proceed as required.
提供自定义跟踪行为(例如,更改突出显示外观)为此,请覆盖以下一项或全部方法:beginTrackingWithTouch:withEvent:
,continueTrackingWithTouch:withEvent:
, endTrackingWithTouch:withEvent:
.
To provide custom tracking behavior (for example, to change the highlight appearance) To do this, override one or all of the following methods: beginTrackingWithTouch:withEvent:
, continueTrackingWithTouch:withEvent:
, endTrackingWithTouch:withEvent:
.
第一部分是让您的 UIControl
子类为您的控件的客户端或用户执行目标操作处理的非标准处理(尽管这听起来不像您想要做的)你真的没有给出高层次的描述).
The first part is for having your UIControl
subclass do non-standard handling of target action processing for clients or users of your control (that doesn't sound like what you are trying to do, though you didn't really give a high-level description).
第二部分听起来更像是您想要做的 - UIControl
子类中的自定义跟踪.
The second part sounds more like what you are wanting to do - custom tracking within your UIControl
subclass.
这篇关于UIControl 子类 - 事件调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!