我想要在UISlider
附加UIButton
的情况下连续更改UILongPressGestureRecognizer
的值。现在,我只接到我的UILongPressGestureRecognizer
代表的电话,然后触摸起来(开始/结束)。
我是否可以在不占用用户界面的情况下从UIGestureRecognizerStateBegan
执行操作UIGestureRecognizerStateEnded
?正如所料,使用while()
循环不起作用。
答案 0 :(得分:3)
以下是如何完成您所需要的工作示例。我测试了它,效果很好。
所有这些代码都在* .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
希望这有帮助!