IOS Pinch放大/缩小

时间:2015-11-18 23:03:00

标签: ios uipinchgesturerecognizer

我正在尝试在相机视图上实现缩放放大/缩小。

到目前为止我有这个代码:

- (void) setZoom:(float)zoomValue{

NSError *error = nil;
NSLog(@"Current zoom level %f  " , zoomValue);
if([_device lockForConfiguration:&error]){
    if(zoomValue > 1 ){
        if(zoomValue < _device.activeFormat.videoMaxZoomFactor){
            _device.videoZoomFactor = zoomValue;

            [_device unlockForConfiguration];
        }else{
            _device.videoZoomFactor = _device.activeFormat.videoMaxZoomFactor;
            //self.currentZoom = _device.activeFormat.videoMaxZoomFactor;
            [_device unlockForConfiguration];

        }
    }else if(zoomValue < 1){
        _device.videoZoomFactor = 1;
        //self.currentZoom = 1;
        [_device unlockForConfiguration];
    }
}

}

这是捏手势处理程序的代码:

-(IBAction)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer {

    if ( pinchGestureRecognizer.state == UIGestureRecognizerStateBegan )
    {
        _zoomGestureCurrentZoom = pinchGestureRecognizer.scale;
    }
    else if ( pinchGestureRecognizer.state == UIGestureRecognizerStateChanged )
    {
        // we have to jump through some hoops to clamp the scale in a way that makes the UX intuitive
        float scaleDeltaFactor = pinchGestureRecognizer.scale/_zoomGestureLastScale;
        float currentZoom = self.zoomGestureCurrentZoom;
        float newZoom = currentZoom * scaleDeltaFactor;
        // clamp
        float kMaxZoom = 6.0f;
        float kMinZoom = 1.0f;
        newZoom = MAX(kMinZoom,MIN(newZoom,kMaxZoom));
        //self.view.transform = CGAffineTransformScale([[gestureRecognizer view] transform], newZoom, newZoom);
        [_imageCameraSource setZoom:newZoom];
        // store for next time
        self.zoomGestureCurrentZoom = newZoom;
     }
}

我遇到的问题很少:

  1. 相机会不时冻结,我认为这是因为传递了无效的变焦值,但无法弄明白。
  2. 每次我开始捏合手势时,它都从正常尺寸开始,然后增加,因此缩小功能实际上并不起作用。
  3. 如果有人有任何工作代码,最好看一看并弄清楚整个功能或欢迎任何其他建议。

    由于

0 个答案:

没有答案