我想在持有带有 a的 aUISlider
时连续更改 a 的值。现在我只在触地和触地(开始/结束)时接到我的代表的电话。UIButton
UILongPressGestureRecognizer
UILongPressGestureRecognizer
我可以在不绑定 UI的情况下执行从UIGestureRecognizerStateBegan
到到的操作吗?UIGestureRecognizerStateEnded
正如预期的那样,使用while()
循环不起作用。
我想在持有带有 a的 aUISlider
时连续更改 a 的值。现在我只在触地和触地(开始/结束)时接到我的代表的电话。UIButton
UILongPressGestureRecognizer
UILongPressGestureRecognizer
我可以在不绑定 UI的情况下执行从UIGestureRecognizerStateBegan
到到的操作吗?UIGestureRecognizerStateEnded
正如预期的那样,使用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
希望这可以帮助!