gpt4 book ai didi

objective-c - 如何检测屏幕上没有手指?

转载 作者:行者123 更新时间:2023-12-01 17:58:09 25 4
gpt4 key购买 nike

我需要touchesEnded函数的帮助。我想使用NSTimer函数在屏幕上没有手指时启动touchesEnded。这可能吗?。目前,我有一个touchesBegan函数正在工作:-)。

这是我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger numTaps = [[touches anyObject] tapCount];

if (numTaps >= 2)
{
self.label.text = [NSString stringWithFormat:@"%d", numTaps];
self.label2.text = @"+1";
[self.button setHidden:YES];
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void)showButton
{
[self.button setHidden:NO];
}

最佳答案

这很棘手。您不会得到一个告诉您所有手指都向上的事件。每个touchesEnded仅告诉您该手指向上。因此,您必须自己跟踪触摸。通常的技术是在触摸到达时将其存储在可变集中。但是您不能自己存储触摸,因此必须存储一个标识符。

首先在UITouch上放置一个类别,以派生此唯一标识符:

@interface UITouch (additions)
- (NSString*) uid;
@end

@implementation UITouch (additions)
- (NSString*) uid {
return [NSString stringWithFormat: @"%p", self];
}
@end

现在,我们必须在有触摸的整个过程中保持可变的设置,在第一次触摸时创建它,并在最后一次触摸消失时销毁它。我假设您有一个NSMutableSet实例变量/属性。这是伪代码:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// create mutable set if it doesn't exist
// then add each touch's uid to the set with addObject:
// and then go on and do whatever else you want to do
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// remove uid of *every* touch that has ended from our mutable set
// if *all* touches are gone, nilify the set
// now you know that all fingers are up
}

关于objective-c - 如何检测屏幕上没有手指?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995021/

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