UIImageView的旋转移动缩放在旋转180°后反向移动

时间:2013-11-20 10:30:07

标签: ios iphone objective-c uiimageview uipinchgesturerecognizer

以下是移动,缩放和旋转用户界面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];

}

2 个答案:

答案 0 :(得分:0)

2个选项:

  1. 使用CGAffineTransformTranslate与使用ScaleRotate的方式相同。
  2. 更好的是,重置仿射变换,移动图像视图,然后重新设置变换。
  3. 尝试使用以下选项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]];