温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS扫描二维码如何实现手势拉近拉远镜头

发布时间:2021-08-06 14:03:15 来源:亿速云 阅读:158 作者:小新 栏目:移动开发

这篇文章将为大家详细讲解有关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扫描二维码如何实现手势拉近拉远镜头”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios
AI