如何在保持前一次旋转变换的同时进行变焦?

时间:2012-02-24 15:10:11

标签: iphone ios uigesturerecognizer

我有一个imageView,我已经添加了UIPinchGestureRecognizer和UIRotationGestureRecognizer。

在捏合手势中,我变换并缩放视图,并且在旋转手势中,我将旋转变换应用于它。

问题是当我旋转imageView然后开始缩放时。缩放始终从正常状态开始。

所以我想要的是当我将它旋转到顺时针旋转30度然后缩放它时。它应该在顺时针方向保持30度的同时进行变焦。

以下是代码:

- (void)viewDidLoad{
    [super viewDidLoad];

//setting up the image view

mTotalRotation = 0.0;
self.imageView.image = self.photo;
self.imageView.userInteractionEnabled = YES;

UIRotationGestureRecognizer *twoFingersRotate = 
[[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
[self.imageView addGestureRecognizer:twoFingersRotate];

UIPinchGestureRecognizer *pinchGesture = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchZoom:)] autorelease];
[self.imageView addGestureRecognizer:pinchGesture];

// Do any additional setup after loading the view from its nib.
}
// Rotation gesture handler
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer 
{


if ([recognizer state] == UIGestureRecognizerStateEnded) {

    mTotalRotation += recognizer.rotation;
    return;
}

self.imageView.transform = CGAffineTransformMakeRotation(mTotalRotation + recognizer.rotation);         


}


   // Pinch Gesture

   -(void)pinchZoom:(UIPinchGestureRecognizer*)recognizer{


    self.imageView.transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale) ;



}

2 个答案:

答案 0 :(得分:4)

更改行:

self.imageView.transform = CGAffineTransformMakeRotation(mTotalRotation + recognizer.rotation);

使用:

self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, recognizer.rotation);

这一行:

self.imageView.transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);

使用:

self.imageView.transform = CGAffineTransformScale(self.imageView.transform, recognizer.scale, recognizer.scale);

修改

要限制比例,您可以执行以下操作:

CGAffineTransform transform = self.imageView.transform;
float newScale = recognizer.scale * sqrt(transform.a*transform.a + transform.c*transform.c);
if (newScale > scaleLimit) {
    self.imageView.transform = CGAffineTransformScale(transform, recognizer.scale, recognizer.scale);
}

答案 1 :(得分:0)

也许您可以将缩放和旋转视图作为缩放视图的子视图?