gpt4 book ai didi

objective-c - 目标 Action uicontrolevents

转载 作者:太空狗 更新时间:2023-10-30 03:20:47 26 4
gpt4 key购买 nike

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

UIControl 有一个方法

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

它允许您添加一个 Action ,当任何给定的 controlEvent 发生时调用。 ControlEvents 是事件的位掩码,它告诉您触摸是否向下、向上或被拖动等,大约有 16 个事件,您或它们一起发生,并在其中任何一个发生时被调用。

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

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

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

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

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

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

最佳答案

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

希望这对您有所帮助!干杯/J

UIControl+CaptureUIControlEvents

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

问题:触发时,UIControlEvents 未传递到目标操作分配给特定事件。这将是有用的,以便只有一个基于触发的 UIControlEvent 切换的 Action 。

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

问题:但是我们不能覆盖私有(private) API,所以:(更糟糕的)解决方案:让 UIControl 存储上次触发的 UIControlEvent。

UIControl 文档指出:

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

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

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

使用示例:

在 UIControl 设置上:

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

目标 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;
}

关于objective-c - 目标 Action uicontrolevents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2437875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com