十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章將為大家詳細講解有關iOS掃描二維碼如何實現(xiàn)手勢拉近拉遠鏡頭,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在做掃碼需求,往往會有放大鏡頭需求。
蘋果提供了AVCaptureConnection中,videoScaleAndCropFactor:縮放裁剪系數(shù),使用該屬性,可以實現(xiàn)拉近拉遠鏡頭。再結合手勢UIPinchGestureRecognizer,就很簡單實現(xiàn)手勢拉近拉遠鏡頭。
手勢代碼
///記錄開始的縮放比例 @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掃描二維碼如何實現(xiàn)手勢拉近拉遠鏡頭”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。