简体   繁体   中英

Performance difference between setting frame.origin vs CGAffineTransformMakeTranslation

I have a grid-like thumbnail scroll view where I am firstly initializing all thumbnails and then setting its frame with:

thumbnail.frame = CGRectMake(0, 0, 100, 100);

then later on willAnimateRotationToInterfaceOrientation , repositioning these thumbnails with:

thumbnail.transform = CGAffineTransformMakeTranslation(posX, posY);

although when checking the position after the method above:

NSLog(@"thumbnail frame x: %f", thumbnail.frame.origin.x);

it will return the position including the transform operated and not 0,0

Is there is a real benefit on using transform instead setting a new frame for an UIView as a reposition purpose?

The main benefit is that you can use more types of Affine Transformations, like

  • CGAffineTransformTranslate
  • CGAffineTransformScale
  • CGAffineTransformRotate
  • CGAffineTransformInvert
  • CGAffineTransformConcat

but frame struct only sets the frame.

Other notes:

1) In performance reasons assuming that CGAffineTransformMakeTranslation(posX, posY) changes only center property it is loos like it should work faster than changing the frame property but who knows. So for reposition purposes I would use this way or changing the center property.

2) In contrast of CALayer 's transform the UIView 's transform changes real position of the view.

Using the translation transform will move (translate) the view in the transformed perspective. That is, uf you first rotate the view, and then translates it, it will move in the direction relative to the rotation. For a simple reordering, I would suggest changing the center property instead. This way, you can still use the frame property in a meaningful way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM