Objective-C,iPhone iOS 6.0
下面的代码很好地支持纵向方向,但是当我尝试横向时,捏缩放自动将其旋转回尴尬的纵向模式(但在横向视图中)。
@implementation AVCapture
{
CGFloat beginGestureScale;
CGFloat effectiveScale;
}
- (id)initWithView:(UIView *)cameraView
{
......
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
[self rotateToInterfaceOrientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
self.previewLayer.frame = cameraView.frame;
[self.previewLayer setPosition:CGPointMake(CGRectGetMidX(cameraView.bounds), CGRectGetMidY(cameraView.bounds))];
[cameraView.layer addSublayer:self.previewLayer];
effectiveScale = 1.0;
......
}
- (void)rotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
CALayer *previewLayer = self.previewLayer;
switch (toInterfaceOrientation)
{
case UIDeviceOrientationLandscapeLeft:
previewLayer.transform = CATransform3DMakeRotation(M_PI+M_PI_2, 0, 0, 1); // 270 degress
break;
case UIDeviceOrientationLandscapeRight:
previewLayer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1); // 270 degress
break;
case UIDeviceOrientationPortraitUpsideDown:
previewLayer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1); // 270 degress
break;
default:
previewLayer.transform = CATransform3DMakeRotation(0.0, 0, 0, 1); // 270 degress
break;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#pragma mark Pinch to Zoom Gestures
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ( [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] )
beginGestureScale = effectiveScale;
return YES;
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer
{
if ([recognizer numberOfTouches] < 2)
return;
BOOL allTouchesAreOnThePreviewLayer = YES;
NSUInteger numTouches = [recognizer numberOfTouches], i;
for (i=0; i<numTouches; ++i)
{
CGPoint location = [recognizer locationOfTouch:i inView:m_CameraView];
CGPoint convertedLocation = [self.previewLayer convertPoint:location fromLayer:self.previewLayer.superlayer];
if (![self.previewLayer containsPoint:convertedLocation])
{
allTouchesAreOnThePreviewLayer = NO;
break;
}
}
if (allTouchesAreOnThePreviewLayer)
{
effectiveScale = beginGestureScale * recognizer.scale;
if (effectiveScale < 1.0)
effectiveScale = 1.0;
CGFloat maxScaleAndCropFactor = [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor];
if (effectiveScale > maxScaleAndCropFactor)
effectiveScale = maxScaleAndCropFactor;
[CATransaction begin];
[CATransaction setAnimationDuration:.025];
// Scale
[self.previewLayer setAffineTransform:CGAffineTransformMakeScale(effectiveScale, effectiveScale)];
[CATransaction commit];
}
}
我似乎无法弄清楚如何保持我的转变。