设置 UILongPressGesture 的最长持续时间?

Set maximum duration for UILongPressGesture?

我有一个带保持操作的按钮。我使用 UILongPressGestureRecognizer。我想从保持操作中获得最大值。

执行按钮时,我会启动 CABasicAnimation 30 秒并开始录制最多 30 秒的视频。如果按钮在 30 秒之前没有释放,我想要 UIGestureRecognizerStateEnded 的执行方法。一些想法,好吗?谢谢!

这是我的部分代码:

 UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc]
                                                               initWithTarget:self action:@selector(handleBtnLongPressGesture:)];
        [self.snapButton addGestureRecognizer:btn_LongPress_gesture];

长按方法:

- (void)handleBtnLongPressGesture:(UILongPressGestureRecognizer *)recognizer {

    //as you hold the button this would fire
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        if(!self.camera.isRecording) {

            self.flashlightButton.hidden = YES;
            self.flshlightLabel.hidden=YES;
            self.switchCameraButton.hidden = YES;
            self.switchImage.hidden=YES;
            self.FlashImage.hidden=YES;
            self.BackImage.hidden=YES;



            // start recording
            NSURL *outputURL = [[[self applicationDocumentsDirectory]
                                 URLByAppendingPathComponent:@"test1"] URLByAppendingPathExtension:@"mov"];
            [self.camera startRecordingWithOutputUrl:outputURL];
//            self.snapImage.hidden=YES;
            circle = [CAShapeLayer layer];

            // Make a circular shape
            circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 73, 73)
                                                     cornerRadius:37].CGPath;

            circle.frame=CGRectMake(self.snapImage.frame.origin.x + 3, self.snapImage.frame.origin.y + 3, 74, 74);


            // Configure the apperence of the circle
            circle.fillColor = [UIColor clearColor].CGColor;
            circle.strokeColor = [UIColor colorWithRed:149.0/256.0 green:153.0/256.0 blue:150.0/256.0 alpha:247.0/256.0].CGColor;
            circle.lineWidth = 8;
            // Add to parent layer
            [self.view.layer addSublayer:circle];

            drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

                drawAnimation.duration            = 30.0; // "animate over 10 seconds or so.."
            // "animate over 10 seconds or so.."
            drawAnimation.repeatCount         = 1.0;  // Animate only once..
            //    drawAnimation.removedOnCompletion = YES;   // Remain stroked after the animation...


            // Animate from no part of the stroke being drawn to the entire stroke being drawn
            drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
            drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];

            // Experiment with timing to get the appearence to look the way you want
            drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

            // Add the animation to the circle
            [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];


            [self.view bringSubviewToFront:self.snapImage];
        }
    }

    // as you release the button this would fire
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        if(self.camera.isRecording) {
            self.flashlightButton.hidden = NO;
            self.flshlightLabel.hidden=NO;
            self.switchCameraButton.hidden = NO;
            self.switchImage.hidden=NO;
            self.FlashImage.hidden=NO;
            self.BackImage.hidden=NO;
            [circle removeAnimationForKey:@"drawCircleAnimation"];
            [circle removeFromSuperlayer];
            [self.camera stopRecording:^(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error) {
                VideoViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VideoVC"];
                vc.urlVideo=outputFileUrl;
                [self presentViewController:vc animated:YES completion:nil];
            }];
        }
    }
}

创建一个 NSTimer 属性 调用计时器:@property (nonatomic, strong) NSTimer *timer;

还有一个计数器:@property (nonatomic, strong) int counter;

   - (void)incrementCounter {
    self.counter++;
}

- (void)handle:(UILongPressGestureRecognizer)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
         self.counter = 0;
         self.timer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(incrementCounter) userInfo:nil repeats:yes];
    }
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [self.timer invalidate];
    }
}

并在 30 秒后移除手势

longPressgesture.enable = NO;