iosobjective-ccgaffinetransformscale

IOS/Objective-C:Animation of Multiple CGAffineTransformations


I am trying to perform an animation that does three things at once: translates, rotates and changes the size of an image.

I can do two at once, translate and size. However, when I add in rotation at the end of the following code, it is ignored. And if I place it at the beginning of the code, the size change is ignored. I've read that you can do a composite transition with view.transform, however, I have not been able to get that to work.

Here is my current code:

CGPoint destPoint = CGPointMake(-100,-50);
 float radians =[self Degrees2Radians:-35];
 [UIView animateWithDuration:2 
                 animations:^{
                     //TRANSLATE
                     self.imageView.center = CGPointMake(self.imageView.center.x + destPoint.x, self.imageView.center.y + destPoint.y);

                     //ROTATE
                      self.imageView.transform = CGAffineTransformMakeRotation(radians);

                  //SCALE

                     self.imageView.transform = CGAffineTransformMakeScale(0.2, 0.2); // here the final size will be 20%

                 }
                 completion:nil
 ];

}

Can anyone recommend way to get all three things to occur simultaneously.

Here is some code for swift that uses the transform property of the view, but I have not been able to find the equivalent in Objective-C.

view.transform= CGAffineTransform(scaleX: 1.5, y: 1.5)
view.transform = view.transform.rotated(by angle: CGFloat(45 * M_PI / 180))

Thanks in advance for any suggestions.


Solution

  • You can use CGAffineTransformRotate function on the existing transform to apply a rotation. You can also use CGAffineTransformTranslate and CGAffineTransformScale to apply translation and scaling. Please note that, order of the operations matter.

    For example if you have an existing transform myTransform you can rotate it like:

    myTransform = CGAffineTransformRotate(myTransform, M_PI / 2);

    The operation does not affect the input variable, instead, it returns a new transform so make sure you use the return value of the function. That's why I started the line with myTransform = ....

    More information is available at https://developer.apple.com/documentation/coregraphics/cgaffinetransform-rb5?language=objc.