2014-07-14 27 views
1

我在用戶打開應用程序時正在加載cameraview,在這裏我需要顯示掃描動畫,如條形碼掃描。有什麼方法可以使用AVCaptureSession在cameraView上顯示動畫。如何在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]; 
+0

是的,您可以在相機視圖上方設置覆蓋視圖,併爲複製條形碼的幾組圖像添加動畫掃描 – Blisskarthik

+0

是的,我知道這一點。但我正在尋找展示掃描動畫代碼 – user3743552

回答

0

創建一個scanninglayer和這個子層添加到預覽層,使用圖像的陣列,用於掃描動畫的持續時間

例如:

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; 
[email protected]"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]; 
+0

這一行將做什麼layerAnimation1.beginTime = startstimeone-gmtime;?我正在使用未聲明的 – user3743552

+0

在這裏你只是給開始時間作爲開始時間刪除這些startingtimeone-gmtime – falcon143

+0

我有圖像名稱line.png。我需要將此圖像顯示爲從下到上的掃描動畫 – user3743552

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"];