UILongPressGestureRecognizer

UILongPressGestureRecognizer

我想在保持附加了UISliderUIButton的同时连续更改UILongPressGestureRecognizer的值。现在,我只能在上下触摸(开始/结束)时接到我的UILongPressGestureRecognizer委托的电话。

是否可以在不占用UI的情况下执行从UIGestureRecognizerStateBeganUIGestureRecognizerStateEnded的操作?不出所料,使用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/

10-09 06:30