我有一个按住Action的按钮。我使用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];
            }];
        }
    }
}

最佳答案

创建一个名为timer的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;

09-30 12:50