gpt4 book ai didi

iphone - 在 UILongPressGestureRecognizer 持续时间内执行操作

转载 作者:行者123 更新时间:2023-12-01 19:06:23 24 4
gpt4 key购买 nike

我想更改 UISlider 的值不断地,而 UIButtonUILongPressGestureRecognizer附属于它正在举行。现在我只接到我的UILongPressGestureRecognizer的电话代表触地和触地(开始/结束)。

我可以从 UIGestureRecognizerStateBegan 执行操作吗?至UIGestureRecognizerStateEnded不绑定(bind)用户界面?正如所料,使用 while()循环不起作用。

最佳答案

这是一个工作示例,说明如何完成您正在寻找的内容。我对其进行了测试并且效果很好。

所有这些代码都在 *.m 文件中。这是一个非常简单的类,它只是扩展了 UIViewController .

#import "TSViewController.h"

@interface TSViewController ()

@property (nonatomic, strong) NSTimer *longPressTimer;

@end

@implementation TSViewController

- (void)viewDidLoad
{
[super viewDidLoad];

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
[self.view addGestureRecognizer:longPress];
}

-(void)longPressGesture:(UILongPressGestureRecognizer*)longPress {

// The long press gesture recognizer has been, well, recognized
if (longPress.state == UIGestureRecognizerStateBegan) {

if (self.longPressTimer) {
[self.longPressTimer invalidate];
self.longPressTimer = nil;
}

// Here you can fine-tune how often the timer will be fired. Right
// now it's been fired every 0.5 seconds
self.longPressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(longPressTimer:) userInfo:nil repeats:YES];
}

// Since a long press gesture is continuous you have to detect when it has ended
// or when it has been cancelled
if (longPress.state == UIGestureRecognizerStateEnded || longPress.state == UIGestureRecognizerStateCancelled) {
[self.longPressTimer invalidate];
self.longPressTimer = nil;
}
}

-(void)longPressTimer:(NSTimer*)timer {

NSLog(@"User is long-pressing");
}

@end

希望这可以帮助!

关于iphone - 在 UILongPressGestureRecognizer 持续时间内执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19436996/

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