33

Is there any problem applying multiple transforms to a UIView and it's corresponding CALayer?

Specifically, can you "mix and match" CATransform3Ds with CGAffineTransforms without running into issues?

Also are there any problems with setting some transforms directly while animating another transform change simultaneously?

Are there any "rules" for how this should be done, or any design patterns for this?

Corey Floyd
  • 25,929
  • 31
  • 126
  • 154

4 Answers4

45

I realize this doesn't answer the question entirely (or come close), but if you're only working with CGAffineTransforms you can use CGAffineTransformConcat() to combine multiple transforms.

This will work just fine when some transforms are animated and others are not, as long as you concat the transformations properly. I don't know how this works when you're also doing layer transforms.

pix0r
  • 31,139
  • 18
  • 86
  • 102
  • 2
    `CGAffineTransformConcat` works well for two transforms but is a little akward for three or more. See [this answer](http://stackoverflow.com/questions/30929986/how-to-apply-multiple-transforms-in-swift/30929987#30929987) for doing more than two. – Suragch Jun 19 '15 at 04:52
37

pix0r is right but here is some more info on this. The official docs for CGAffineTransformConcat().

Also, here is a quick example:

// Rotate 45 degrees
CGAffineTransform rotate = CGAffineTransformMakeRotation(45*(M_PI/180));
// Move to the left
CGAffineTransform translate = CGAffineTransformMakeTranslation(-50,0);
// Apply them to a view
self.view.transform = CGAffineTransformConcat(translate, rotate);
mariusLAN
  • 1,195
  • 1
  • 12
  • 26
whitehawk
  • 2,429
  • 29
  • 33
  • 2
    Shouldn't it be CGAffineTransformConcat(rotate, translate), given that the points are row vectors? – Plumenator Aug 01 '11 at 07:11
  • I thought the same, but it appears the translate should be applied first. Rotate is around the center of the translated coordinate system, but if you rotate first then translate, the translate is along the axis of the rotated coordinate system. – gamozzii Oct 11 '12 at 22:52
  • As 2022, rotate and then translate worked for me. – Pedro Paulo Amorim May 24 '22 at 19:20
12

Syntax has changed slightly with Swift 3 & 4. Adaptation of @whitehawk's answer:

// Rotate 45 degrees
var rotate = CGAffineTransform(rotationAngle: 45 * (.pi / 180))
// Move to the left
var translate = CGAffineTransform(translationX: -50, y: 0)
// Apply them to a view
self.view.transform = translate.concatenating(rotate)
Justin Vallely
  • 5,932
  • 3
  • 30
  • 44
-1

I succeed to translate and rotate an imageView in the same time, this way:

float scaleFactor_x   = 2.8;
float scaleFactor_y   = 2.45;
imgBigBallBasic.frame = CGRectMake(112, 20, 100, 100);
CGAffineTransform scaleTrans  = CGAffineTransformMakeScale(scaleFactor_x, scaleFactor_y);
CGAffineTransform translateTrans  = CGAffineTransformMakeTranslation(0, 55);
imgBigBallBasic.contentMode = UIViewContentModeScaleAspectFit;
imgBigBallBasic.transform = CGAffineTransformConcat(translateTrans, scaleTrans);
imgBigBallBasic.frame = CGRectMake(112, 20, 100, 100);
imgBigBallBasic.center = [[imgBigBallBasic window] center];
[UIView commitAnimations];