0

I may be misunderstanding how the CGAffineTransform works but it seems to be giving strange results for the origin of the frame.

for example :

        print(attribute.frame)
        attribute.transform = CGAffineTransform(scaleX: 0.68, y: 0.68)
        print(attribute.frame)

gives the results :

(213.0, 54.0, 459.0, 23.5)

(286.29948979591836, 57.75280612244898, 312.4010204081633, 15.994387755102032)

The width and height scale correctly but the x and y origin have increased in value.

Md1079
  • 1,360
  • 1
  • 10
  • 28
  • You may need to use classic ways to concate a bunch of transforms. or use `center` method if it's simple. – E.Coms Mar 27 '19 at 13:22
  • the center property doesn't seem to do much in this instance. As soon as a transform is applied to these layoutAttributes, the frame will be calculated with the transform at run time when displaying the cell. The trick is getting the correct transform that scales the origin correctly, but the Maths involved is a bit weird for me i can't figure out how it's working – Md1079 Mar 27 '19 at 14:40
  • good luck in math. – E.Coms Mar 27 '19 at 14:54

2 Answers2

2

The transform uses the center of your view as anchor point. The result being that the center stays the same, should be (442.5, 65,75) if i calculate correctly but the origin will move (increase in value if you scale down, and decrease if you scale up). There are various techniques to change the anchor point if you want to keep the origin, perhaps take a look at this thread : Scale with CGAffineTransform and set the anchor

SveinnV
  • 184
  • 4
  • would the view in this case be my UICollectionView? I am trying to scale and then translate the transform but not quite there as yet. – Md1079 Mar 27 '19 at 10:31
  • The view is the view you are attaching the transform to, in your case you seem to call it "attribute" – SveinnV Mar 27 '19 at 11:18
  • this is a UICollectionViewLayoutAttributes object. it seems to work independently of views, when you apply a transform it will then scale the UICollectionView cells content and its position to create a zooming effect.. unfortunately I cant seem to get the position to behave – Md1079 Mar 27 '19 at 11:19
  • I haven't worked with translations on UICollectionViewLayoutAttributes so I can't really be sureI but I would assume that the transform should work the same with that as any other "view", you should just have to figure the correct translation to move the origin back to it's original point after scaling. – SveinnV Mar 27 '19 at 11:46
1

I thinks the transform must be apply to the center of rect.
I don't know what is the type of attribute. Maybe there is something called anchor inside attribute. You can try to change the property.

Frank Cheng
  • 5,928
  • 9
  • 52
  • 80
  • it is a UICollectionViewLayoutAttributes object. yes I could try and play about with the anchor point. – Md1079 Mar 27 '19 at 09:49