![](/img/trans.png)
[英]Perform Action after duration if buttonIndex of alertView is not clicked
[英]Perform action for duration of UILongPressGestureRecognizer
我想改變的值UISlider
而連續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
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.