24

I have a few UIImageViews inside CALayer. How do I scale them all at once? I did try setting CALayer properties like la.contentsGravity = @"kCAGravityResizeAspect"; but the images inside the layer always display the original size.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Registered User
  • 3,669
  • 11
  • 41
  • 65

1 Answers1

70

if layer contains the image, this will scale it to 65%

layer.transform = CATransform3DMakeScale(.65, .65, 1);
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • Why does that do nothing to mine when it leave the third number 1 and start working when I also set that .65 just like the first and the second? – durazno May 22 '16 at 17:56
  • @durazno: applying a 3-dimensional transform on a 2-dimensional structure — the third parameter (z) is irrelevant. if you would rotate around the z-axis prior to this code, using another value than 1 should result in different results. – vikingosegundo Sep 23 '21 at 17:42
  • This worked bot only **after** I added the layer as a sublayer. Eg. `view.layer.addSublayer(myLayer)`; `myLayer.transform = CATransform3DMakeScale(0.65, 0.65, 1)` – Lance Samaria Apr 25 '22 at 19:51
  • @LanceSamaria hence the first sentence "if layer contains the image" – vikingosegundo Apr 26 '22 at 10:08
  • @vikingosegundo hi, thanks for replying. **Your answer 100% works, I even upvoted it**. But when I added the image to the layer first, did the transform second, then added the layer to the view's layer third, the transform didn't occur. It only worked for me in this order: add the image to the layer first, add the layer to the view's layer second, and transform third. Again, you answer definitely works, but adding the image to the layer first and doing the transform before adding the layer to the view didn't work for me. – Lance Samaria Apr 27 '22 at 00:24
  • why would you expect that to work? it doesn't make any sense. – vikingosegundo Apr 27 '22 at 10:54