gpt4 book ai didi

ios - 使用 touchesBegins 的问题

转载 作者:行者123 更新时间:2023-11-28 22:26:33 25 4
gpt4 key购买 nike

我有一个按钮。我想在初始触摸时更改图像。如果触摸时间少于 1 秒,我希望它执行 X。如果触摸时间长于 1 秒,我希望它执行 Y。

我不知道如何处理这个问题。事实证明 UIButton 很麻烦,所以我想我可以使用 UIGestureRecognizerstouchesBegin:

最初的想法是使用 UITapGestureRecognizer 来检测快速点击以执行 X,并使用 UILongTapGestureRecognizer 来处理长按以执行 Y。

问题是 UITapGestureRecognizer 没有标记 UIGestureRecognizerStateBegan,它只会为 UIGestureRecognizerStateEnd 发送通知。

所以我决定尝试组合覆盖 touchesBegin:touchesEnd: 方法并使用 UILongPressGestureRecognizer:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// change image
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// do X
// change image to original image
}

-(IBAction)longPressDetected:(UILongPressGestureRecognizer *)recognizer {
DLog(@"fired");

if (recognizer.state == UIGestureRecognizerStateBegan) {
// Do y
// change image to original image
}
else if (recognizer.state == UIGestureRecognizerStateCancelled) {

}
else if (recognizer.state == UIGestureRecognizerStateEnded) {

}
}

如果 UILongPressGestureRecognzier 触发,它会取消初始 touchesBegan:(不会触发 touchesEnded: 方法)。

但我遇到了 touchesBegin: 方法启动缓慢的问题。触发方法有 0.5 秒的延迟。让我感到困惑的是,如果我将 UILongPressGestureRecognizerlongTap.minimumPressDuration = 0 一起使用,它会立即触发。

这是在我需要的程序中。在虚拟区域中使用它,touchesBegins: 也会立即触发。

什么会导致它在程序中滞后?

是否有其他方法可以获得所需的效果?

最佳答案

也许你可以使用计时器和长按手势识别器

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeLongPressGesture:)];
longPress.minimumPressDuration = 0;
[self.button addGestureRecognizer:longPress];

...

- (void)didRecognizeLongPressGesture:(UILongPressGestureRecognizer*)gesture
{
static NSTimer *timer = nil;

if (gesture.state == UIGestureRecognizerStateBegan)
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.1 target:self selector:@selector(changeImage:) userInfo:nil repeats:NO];
//change the image here
}
else if ( gesture.state == UIGestureRecognizerStateEnded)
{
if ([timer isValid])
{
//the user has pressed the button less than a second
[timer invalidate];
timer = nil;
}

}

}

- (void)changeImage:(NSTimer*)timer
{
[timer invalidate];
timer = nil;

//change to the other image here
}

关于ios - 使用 touchesBegins 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18795810/

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