2015-07-28 81 views
1

我有一个按钮与持有行动。我使用UILongPressGestureRecognizer。我想从持有行动中获得最大价值。为UILongPressGesture设置最长持续时间?

当执行按钮我开始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

创建一个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; 
相关问题