Search code examples
iphoneobjective-ccgaffinetransform

How to use Rotation and Translation for an Image simultaneously in iPhone?


I using CGAffineTransformMakeRotation for the Rotation of an Image and CGAffineTransformMakeTranslation for the Translation of an Image. This is my code:

-(void)rotateTranslate
{
    r++;
    CGAffineTransform transform = CGAffineTransformMakeRotation(r);
    imageView.transform = transform;
    x++;
    y++;
    CGAffineTransform transform1=CGAffineTransformMakeTranslation(x,y);
    imageView.transform= transform1;
    [self performSelector:@selector(rotateTranslate) withObject:self afterDelay:0.2];
}

My Problem is the Image is only Translating it is not Rotating. If I use the Rotate and Translate separately means it works Pretty well. How can I modify my code to work both well?


Solution

  • The problem is that you are making an individual rotation and then translation and setting it. So the rotation is overridden by the translation. You must do something like this,

    CGAffineTransform transform = CGAffineTransformRotate(imageView.transform, 1);
    transform = CGAffineTransformTranslate(transform, 1, 1);
    
    imageView.transform= transform;
    
    [self performSelector:@selector(rotateTranslate) withObject:self afterDelay:0.2];
    

    But this will not animate the rotation. It will just jump from the current transform to the end transform.

    And you don't seem to have an end point defined.