如何在cameraView中显示扫描动画

时间:2014-07-14 08:08:53

标签: ios objective-c avcapturesession

当用户打开应用程序时我正在加载cameraview,在这里我需要显示扫描动画,如条形码扫描。有没有办法使用AVCaptureSession在cameraView上显示动画。

[self setCaptureSession:[[AVCaptureSession alloc] init]];


 [self  addVideoInputFrontCamera:NO]; // set to YES for Front Camera, No for Back camera

 [self  addStillImageOutput];

 [self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] ];



 [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];



 CGRect layerRect = [[[self view] layer] bounds];


 [[self previewLayer]setBounds:layerRect];
 [[self  previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
 [[[self view] layer] addSublayer:[self  previewLayer]];

 [[self captureSession] startRunning];

2 个答案:

答案 0 :(得分:0)

创建一个扫描层并将此子图层添加到预览图层,使用图像阵列扫描动画持续时间

示例:

 CALayer *layerGun=[CALayer layer];
 layerGun.bounds=CGRectMake(0.0, 0.0, 704,500);
 [layerGun setPosition:CGPointMake(512,250)];
 [layerGun setContents:(id)[UIImage imageNamed:@" Right0001.png"].CGImage];
 layerGun.zPosition=5;
 NSString *animationkey=[NSString stringWithFormat:@"animate%d",i+1];
 NSString *animationkey2=[NSString stringWithFormat:@"position%d",i+1];
 CAKeyframeAnimation *layerAnimation1 = [CAKeyframeAnimation animation];
 layerAnimation1.calculationMode = kCAAnimationLinear;
 layerAnimation1.beginTime=begantimeone-bgntime;
 layerAnimation1.duration = 0.3;
 layerAnimation1.delegate=self;
 layerAnimation1.keyPath=@"contents";
 layerAnimation1.values = [NSArray arrayWithObjects:
      (id)[[UIImage imageNamed:@"Right0001.png"] CGImage],
      (id)[[UIImage imageNamed:@"Center0003.png"] CGImage],
      (id)[[UIImage imageNamed:@"Center0004.png"] CGImage],
      (id)[[UIImage imageNamed:@"Center0005.png"] CGImage],
      (id)[[UIImage imageNamed:@"Center0006.png"] CGImage], nil];

 [layerGun addAnimation:layerAnimation1 forKey:animationkey];

答案 1 :(得分:0)

希望这个帮助

      CGPoint start = CGPointMake(startX, 0);
      CGPoint end = CGPointMake(startX, self.view.frame.size.height+10);    
      CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
      animation.delegate = self;
      animation.fromValue = [NSValue valueWithCGPoint:start];
      animation.toValue = [NSValue valueWithCGPoint:end];
      animation.duration = 10*speed;

      //animation.repeatCount = repeatCount;
      animation.autoreverses = NO;
      animation.removedOnCompletion   = YES;
      animation.fillMode = kCAFillModeForwards;

      [layer addAnimation:animation forKey:@"position"];