【问题标题】:target-action uicontrolevents目标动作 uicontrolevents
【发布时间】:2010-03-13 09:38:25
【问题描述】:

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

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;

【问题讨论】:

  • “[...] 你或他们在一起 [...]” - 花了我一点时间来理解他那句话的意思 :)

标签: objective-c events action target


【解决方案1】:

我遇到了同样的问题,并想出了一个解决方案。它不是非常漂亮,但效果很好。它是一个 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: 可以被覆盖(或 子类)来存储标志,但事实并非如此。看起来 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;
}

【讨论】:

    【解决方案2】:

    创建 UIControl 时,为 tag 属性设置一个值。然后在您的操作函数中,您可以使用 [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;
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多