您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關iOS掃描二維碼如何實現手勢拉近拉遠鏡頭,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在做掃碼需求,往往會有放大鏡頭需求。
蘋果提供了AVCaptureConnection中,videoScaleAndCropFactor:縮放裁剪系數,使用該屬性,可以實現拉近拉遠鏡頭。再結合手勢UIPinchGestureRecognizer,就很簡單實現手勢拉近拉遠鏡頭。
手勢代碼
///記錄開始的縮放比例 @property(nonatomic,assign)CGFloat beginGestureScale; ///最后的縮放比例 @property(nonatomic,assign)CGFloat effectiveScale; - (void)cameraInitOver { if (self.isVideoZoom) { UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)]; pinch.delegate = self; [self.view addGestureRecognizer:pinch]; } } - (void)pinchDetected:(UIPinchGestureRecognizer*)recogniser { self.effectiveScale = self.beginGestureScale * recogniser.scale; if (self.effectiveScale < 1.0){ self.effectiveScale = 1.0; } [self.scanObj setVideoScale:self.effectiveScale]; } - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if ( [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] ) { _beginGestureScale = _effectiveScale; } return YES; }
拉近拉遠鏡頭代碼
- (void)setVideoScale:(CGFloat)scale { [_input.device lockForConfiguration:nil]; AVCaptureConnection *videoConnection = [self connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]]; CGFloat maxScaleAndCropFactor = ([[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor])/16; if (scale > maxScaleAndCropFactor) scale = maxScaleAndCropFactor; CGFloat zoom = scale / videoConnection.videoScaleAndCropFactor; videoConnection.videoScaleAndCropFactor = scale; [_input.device unlockForConfiguration]; CGAffineTransform transform = _videoPreView.transform; [CATransaction begin]; [CATransaction setAnimationDuration:.025]; _videoPreView.transform = CGAffineTransformScale(transform, zoom, zoom); [CATransaction commit]; }
有一點需要注意:the videoScaleAndCropFactor property may be set to a value in the range of 1.0 to videoMaxScaleAndCropFactor,videoScaleAndCropFactor這個屬性取值范圍是1.0-videoMaxScaleAndCropFactor,如果你設置超出范圍會崩潰哦!
關于“iOS掃描二維碼如何實現手勢拉近拉遠鏡頭”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。