设置UILongPressGesture的最长持续时间?

时间:2015-07-28 12:00:50

标签: ios objective-c iphone camera uilongpressgesturerecogni

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

1 个答案:

答案 0 :(得分:1)

创建一个名为timer:@property (nonatomic, strong) NSTimer *timer;

的NSTimer属性

还有一个柜台:@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;