Auto Wheel Spin

I am trying to rotate a UIImageView using the center of a CGAffineTransformRotate, but the View rotates around during rotation. If I use CATransform3DRotate to rotate its layer, this will not happen, but as soon as I edit Constraints (changing Constant), the rotated image bounces. Does anyone know how to fix this problem?

This is a screenshot of a rotated image that bounced enter image description here

+6
source share
3 answers

I just heard Back from the Apple support team and they provided a solution for my problem by overwriting layoutSubviews Superview. This allows you to apply a special layout for certain types. It may not be the perfect solution, but it works like a charm, and since Apple suggested it, it should be a good solution if it comes from those who implemented Autolayout

I added a more complete answer here - a similar question.

+1
source

The automatic layout acts on the frame of your UIView. The frame is calculated from your center of view, borders and transformation properties. By default, view.transform is only CGAffineTransformIdentity, and in this configuration, the frame is a rectangle oriented identically to supervision, with the same width and height as the border of the view.

However, when you modify view.transform (or, equivalently, view.layer.transform) as you do, then this breaks the predictable relationship between the center, borders, and frame. For example, if your conversion is a 20-degree rotation, then the frame will no longer have the same width and height as the borders. Now the frame will be (I think ...), whatever rectangle oriented exactly to the supervisor does not contain your original rectangle, which now rotates 20 degrees.

And since the automatic layout acts on the frame property, now it will most likely create a layout that you don't need.

For example, suppose your view represents a circle in a 10x10 frame rectangle, the top left aligned with the caption through spatial restrictions. Then you rotate it 45 degrees. The spinning circle looks the same. But now the frame is the smallest rectangle that your original rectangle can contain when it rotates, and this smallest rectangle is 10 / sqrt (2) x 10 / sqrt (2). When the automatic layout applies top left alignment to this new frame, you will see that your circle is shifted down and to the right. Instead of being visually centered on {5.5}, ​​it will now be centered on {5 / sqrt (2), 5 / sqrt (2)}.

I think that for this you need to either manually adjust the "constant" layout constraint parameter to correct the consequences of your conversion, or similarly redefine alignmentRectForFrame to make the automatic layout work on alignmentRect, which is configured to compensate for your conversion, as well as to determine your initial restrictions to snap to the center of your view, which the transformation cannot influence.

+10
source

I just wrote a short essay on this issue:

How to adjust CALayer anchor point when auto layout is used?

My conclusion is that autodetection and representations do not interact very well. I give two solutions: (a) use only constraints that will not resist the transformation that you intend to apply, or (b) completely exclude the view from autorun without restrictions and set its translatesAutoresizingMask property to YES, so that everything works for this point of view as they did before auto power off.

+3
source

All Articles