1

の付いた を保持したまま、 の値をUISlider連続的に変化させたい。現在、タッチダウンとタッチアップ(開始/終了)でデリゲートに電話をかけるだけです。UIButtonUILongPressGestureRecognizerUILongPressGestureRecognizer

UI を拘束せずにからUIGestureRecognizerStateBeganまでのアクションを実行できますか? UIGestureRecognizerStateEnded予想どおり、while()ループを使用しても機能しません。

4

1 に答える 1

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

お役に立てれば!

于 2013-10-17T21:14:09.900 に答える