以下是移动,缩放和旋转用户界面ImageView
的代码,所有工作正常但问题是当我们旋转ImageView
它旋转但之后它反向移动,就像我向右拖动但是ImageView
还剩下了
旋转后我们得不到当前的中心点,这样当我们拖动中心点时,我们就会旋转ImageView
而不是原来的
这是UIImageView
#pragma mark Manipulation
-(void)scale:(id)sender {
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
{
_lastScale = 1.0;
}
CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = self.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[self setTransform:newTransform];
_lastScale = [(UIPinchGestureRecognizer*)sender scale];
[self showOverlayWithFrame:self.frame];
}
-(void)rotate:(id)sender {
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
_lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (_lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = self.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[self setTransform:newTransform];
_lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
[self showOverlayWithFrame:self.frame];
}
-(void)move:(id)sender {
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
_firstX = [self center].x;
_firstY = [self center].y;
}
translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
[self setCenter:translatedPoint];
[self showOverlayWithFrame:self.frame];
}
答案 0 :(得分:0)
2个选项:
CGAffineTransformTranslate
与使用Scale
和Rotate
的方式相同。尝试使用以下选项2的代码:
CGAffineTransform t = self.transform;
self.transform = CGAffineTransformIdentity;
[self setCenter:translatedPoint];
self.transform = t;
答案 1 :(得分:0)
通过更改此行 - (void)move:(id)发件人刚完成任务
旧版
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self];
正确
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:[self superview]];