目标操作 uicontrol 事件

发布于 2024-08-24 19:32:28 字数 789 浏览 5 评论 0原文

我一定在这里遗漏了一些明显的东西,但是...

UIControl 有一个方法

- (void)addTarget:(id)target action:(SEL)action forControlEvents: (UIControlEvents)controlEvents

,可以让您添加一个在任何给定的 controlEvents 发生时调用的操作。 ControlEvents 是事件的位掩码,它告诉您触摸是否向下、向上或被拖动等,大约有 16 个事件,您或它们一起发生,当其中任何一个发生时就会被调用。

选择器可以具有以下签名之一,

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)

这些签名都不会告诉您控制事件位掩码是什么。 UIEvent 略有不同,它与实际的触摸事件相关,并且(我认为)不包含 UIControlEvent。发送者(UIControl)也没有办法找到控件事件。

我想要一种处理许多控制事件的方法,因为我有一些通用代码,无论发生哪个或哪些事件,但我仍然需要知道 UIControlEvents 用于某些特定处理。

我是否缺少一种方法来找出调用操作时使用了哪些 UIControlEvents 或者我真的必须将我的代码分成

- (void)actionWithUIControlEventX;
- (void)actionWithUIControlEventY;

I must be missing something obvious here but ...

UIControl has a method

- (void)addTarget:(id)target action:(SEL)action forControlEvents: (UIControlEvents)controlEvents

which lets you add an action to be called when any of the given controlEvents occur. ControlEvents are a bitmask of events which tell you if a touch went down, or up inside, or was dragged etc., there's about 16 of them, you or them together and get called when any of them occur.

The selector can have one of the following signatures

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)

none of those tell you what the control event bitmask was. The UIEvent is something slightly different, it's related to the actual touch event and doesn't (I think) contain the UIControlEvent. The sender (UIControl) doesn't have a way to find the control events either.

I'd like to have one method which deals with a number of control events as I have some common code regardless of which event or events happened but I still need to know what the UIControlEvents were for some specific processing.

Am I missing a way to find out what UIControlEvents were used when the action was called or do I really have to separate my code into

- (void)actionWithUIControlEventX;
- (void)actionWithUIControlEventY;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难遇 2024-08-31 19:32:28

我遇到了同样的问题,并提出了解决方案。它并不是非常漂亮,但效果很好。它是一个 UIControl 类别,存储最后一个触发到其自己的标记属性的 UIControlEvent。您可以从下面的链接获取它。为了进一步参考,这里是我的类别中的文档,以更详细地描述正在发生的事情。

希望这有帮助!干杯/J

UIControl+CaptureUIControlEvents

git要点位于: http://gist.github.com/513796

问题:触发后,UIControlEvents 不会传递到目标操作中
分配给特定事件。这将很有用,以便仅
根据触发的 UIControlEvent 进行切换的一项操作。

解决方案:添加一种方法来存储在UIEvent中触发的UIControlEvent。

问题:但我们无法覆盖私有 API,因此:
(更糟糕)解决方案:让 UIControl 存储上次触发的 UIControlEvent。

UIControl 文档指出:

当用户以对应于一个或多个的方式触摸控件时
指定的事件,UIControl 向自身发送 sendActionsForControlEvents:。
这会导致 UIControl 将操作发送到 UIApplication
sendAction:to:from:forEvent: 消息。

人们可能会认为 sendActionsForControlEvents: 可以被覆盖(或者
subclassed)来存储标志,但事实并非如此。看来
sendActionsForControlEvents:主要是客户端触发事件的地方
以编程方式。

相反,我必须建立一个方案来为每个控件注册一个操作
人们想要跟踪的事件。我决定不跟踪所有事件(或者在
所有 UIControls)以提高性能和易用性。

使用示例:

在 UIControl 设置中:

UIControlEvents capture = UIControlEventTouchDown;
capture |= UIControlEventTouchDown;
capture |= UIControlEventTouchUpInside;
capture |= UIControlEventTouchUpOutside;
[myControl captureEvents:capture];
[myControl addTarget:self action:@selector(touch:) forControlEvents:capture];

以及目标操作:

- (void) touch:(UIControl *)sender {
  UIColor *color = [UIColor clearColor];
  switch (sender.tag) {
    case UIControlEventTouchDown: color = [UIColor redColor]; break;
    case UIControlEventTouchUpInside: color = [UIColor blueColor]; break;
    case UIControlEventTouchUpOutside: color = [UIColor redColor]; break;
  }
  sender.backgroundColor = color;
}

I encountered the same problem, and came up with a solution. It's not amazingly pretty, but it works quite well. It is a UIControl category that stores the last UIControlEvent fired to its own tag property. You can get it from the link below. For further reference, here's the doc from my category, for a more detailed description of what's going on.

Hopefully this helps! Cheers/J

UIControl+CaptureUIControlEvents

git gist at: http://gist.github.com/513796

PROBLEM: upon firing, UIControlEvents are not passed into the target action
assigned to the particular event. This would be useful in order to have only
one action that switches based on the UIControlEvent fired.

SOLUTION: add a way to store the UIControlEvent triggered in the UIEvent.

PROBLEM: But we cannot override private APIs, so:
(WORSE) SOLUTION: have the UIControl store the UIControlEvent last fired.

The UIControl documentation states that:

When a user touches the control in a way that corresponds to one or more
specified events, UIControl sends itself sendActionsForControlEvents:.
This results in UIControl sending the action to UIApplication in a
sendAction:to:from:forEvent: message.

One would think that sendActionsForControlEvents: can be overridden (or
subclassed) to store the flag, but it is not so. It seems that
sendActionsForControlEvents: is mainly there for clients to trigger events
programatically.

Instead, I had to set up a scheme that registers an action for each control
event that one wants to track. I decided not to track all the events (or in
all UIControls) for performance and ease of use.

USAGE EXAMPLE:

On UIControl setup:

UIControlEvents capture = UIControlEventTouchDown;
capture |= UIControlEventTouchDown;
capture |= UIControlEventTouchUpInside;
capture |= UIControlEventTouchUpOutside;
[myControl captureEvents:capture];
[myControl addTarget:self action:@selector(touch:) forControlEvents:capture];

And the target action:

- (void) touch:(UIControl *)sender {
  UIColor *color = [UIColor clearColor];
  switch (sender.tag) {
    case UIControlEventTouchDown: color = [UIColor redColor]; break;
    case UIControlEventTouchUpInside: color = [UIColor blueColor]; break;
    case UIControlEventTouchUpOutside: color = [UIColor redColor]; break;
  }
  sender.backgroundColor = color;
}
月朦胧 2024-08-31 19:32:28

创建 UIControl 时,为标记属性设置一个值。然后在您的操作函数中,您可以使用 [sender tag] 确定调用它的 UIControl 的标签。这是一个例子:

-(void)viewDidLoad {
    UIButton *button1 = [[UIButton alloc] initWithFrame(CGRectMake(0.0,0.0,100.0,100.0)];
    button1.tag = 42;
    [button1 addTarget:self action:@selector(actionWithUIControlEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];


    UIButton *button2 = [[UIButton alloc] initWithFrame(CGRectMake(100.0,0.0,100.0,100.0)];
    button2.tag = 43;
    [button2 addTarget:self action:@selector(actionWithUIControlEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];

}
-(void)actionWithUIControlEvent:(id)sender {
     switch([sender tag]) {
     case 42:
         //Respond to button 1
         break;
     case 43:
         //Respond to button 2
         break;
     default:
         break;
     }
 }

When you create your UIControl, set a value for the tag property. Then in your action function, you can determine the tag of the UIControl that called it using [sender tag]. Here's an example:

-(void)viewDidLoad {
    UIButton *button1 = [[UIButton alloc] initWithFrame(CGRectMake(0.0,0.0,100.0,100.0)];
    button1.tag = 42;
    [button1 addTarget:self action:@selector(actionWithUIControlEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];


    UIButton *button2 = [[UIButton alloc] initWithFrame(CGRectMake(100.0,0.0,100.0,100.0)];
    button2.tag = 43;
    [button2 addTarget:self action:@selector(actionWithUIControlEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];

}
-(void)actionWithUIControlEvent:(id)sender {
     switch([sender tag]) {
     case 42:
         //Respond to button 1
         break;
     case 43:
         //Respond to button 2
         break;
     default:
         break;
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文