ios – 如何记录UIControlEvents?

ios – 如何记录UIControlEvents?,第1张

概述我试图弄清楚如何记录UIControlEvents,以便我可以确切地看到我想要添加到我的UIButton的那些. 我似乎无法弄明白.这是我到目前为止所尝试的: [button addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventAllEvents];-(void)but 我试图弄清楚如何记录UIControlEvents,以便我可以确切地看到我想要添加到我的UIbutton的那些.

我似乎无法弄明白.这是我到目前为止所尝试的:

[button addTarget:self action:@selector(buttontouched:forEvent:) forControlEvents:UIControlEventAllEvents];-(voID)buttontouched:(UIbutton *)button forEvent:(UIControlEvents)event{NSLog(@"%s",__PRETTY_FUNCTION__);switch (event) {    case UIControlEventtouchDown:        NSLog(@"UIControlEventtouchDown");        break;    case UIControlEventtouchDownRepeat:        NSLog(@"UIControlEventtouchDownRepeat");        break;    case UIControlEventtouchDragInsIDe:        NSLog(@"UIControlEventtouchDragInsIDe");        break;    case UIControlEventtouchDragOutsIDe:        NSLog(@"UIControlEventtouchDragOutsIDe");        break;    case UIControlEventtouchdragenter:        NSLog(@"UIControlEventtouchdragenter");        break;    case UIControlEventtouchDragExit:        NSLog(@"UIControlEventtouchDragExit");        break;    case UIControlEventtouchUpInsIDe:        NSLog(@"UIControlEventtouchUpInsIDe");        break;    case UIControlEventtouchUpOutsIDe:        NSLog(@"UIControlEventtouchUpOutsIDe");        break;    case UIControlEventtouchCancel:        NSLog(@"UIControlEventtouchCancel");        break;    case UIControlEventValueChanged:        NSLog(@"UIControlEventValueChanged");    default:        break;}}

打印的唯一日志是__PRETTY_FUNCTION__日志.我也试过这个,以防switch语句出现问题:

if (event == UIControlEventtouchDown) {    NSLog(@"UIControlEventtouchDown");}if (event == UIControlEventtouchDownRepeat){    NSLog(@"UIControlEventtouchDownRepeat");}etc...

仍然没有记录.我究竟做错了什么?

编辑:我通过更改添加到按钮的事件进行了进一步的实验,如下所示:

[button addTarget:self action:@selector(buttontouched:forEvent:) forControlEvents:UIControlEventtouchDown];

然后我非常具体地记录了输入:

-(voID)buttontouched:(UIbutton *)button forEvent:(UIControlEvents)event{    NSLog(@"UIControlEventtouchDown = %i",UIControlEventtouchDown);    NSLog(@"event = %i",event);}

这是输出:

UIControlEventtouchDown = 1event = 350486320

有谁知道发生了什么事?

编辑2:感谢TAKeanice,我现在意识到我没有正确地编写选择器.我不确定事件的日志是什么打印出来的,但是当我基本上将UIEvent转换为枚举类型(UIControlEvents)时,它仍然在日志中给出一个值.
无论如何,在我看来,仍然有办法找出UIControlEvents是什么,但我无法从文档中看到.
但是,Apple在UIControl文档中提到了它:

Subclassing Notes
You may want to extend a UIControl subclass for either of two reasons:

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.

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:

当为sendAction:to:forEvent:传入的参数是UIEvent时,如何评估UIControlEvents位掩码?
有任何想法吗?

解决方法 试试这个:

// somewhere[self.btn addTarget:self action:@selector(buttontouched:forEvent:) forControlEvents:UIControlEventAllEvents];- (voID)buttontouched:(UIbutton*)sender forEvent:(ID)event {    NSSet *touches = [event alltouches];    UItouch *touch = [touches anyObject];    UItouchPhase phase = touch.phase;}

更新:
你将获得接触,然后在最后一次触摸中你会有阶段,这表明这是什么事件

说明:

从触摸的位置和阶段,您将能够计算事件:

> UItouchPhaseEnded触摸位置在bounds =>范围内UItouchUpInsIDe> UItouchPhaseEnded触摸位置超出边界=> UItouchUpOutsIDe> ……

总结

以上是内存溢出为你收集整理的ios – 如何记录UIControlEvents?全部内容,希望文章能够帮你解决ios – 如何记录UIControlEvents?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1041380.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存