How can I rotate and move a UIView at the same tim

2019-09-17 06:49发布

I'm trying to make a UIView move and rotate at the same time.

Here's my code:

_viewToDrag.frame = CGRectMake(x, y, size, width);
_viewToDrag.transform = CGAffineTransformMakeRotation(-M_PI_2 * multiplier));

As the multiplier increases (0.0 .. 1.0) the view stretches beyond any logic.

This post seems to answer my question: Rotating and Moving a UIImageView (CocoaTouch)

But, since I'm code-dyslexic and a bit retarded can some explain what this translates to code:

Change the "center" property instead.

I would have made a comment but my reputation doesn't allow it.

2条回答
可以哭但决不认输i
2楼-- · 2019-09-17 07:16

Always consult the documentation. Apple says, in a big box with an exclamation mark graphic next to it, in the section on frame:

Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

Underneath centre it says:

The center is specified within the coordinate system of its superview and is measured in points. Setting this property changes the values of the frame properties accordingly.

So the answer you link to is incorrect. Given its one-sentence nature with no reference to sources, probably the author tried it once, on the version of iOS he happened to have installed, it looked like it worked and he jumped straight to an unsupported conclusion.

The other answer is correct. You need to build the translation into your transform. Likely by throwing in a use of CGAffineTransformTranslate.

EDIT: so, to set a transform with a translation of (1000, 0) and a rotation of -M_PI/2:

_viewToDrag.transform = CGAffineTransformRotate(
    CGAffineTransformMakeTranslation(1000.0, 0.0),
    -M_PI_2);
查看更多
欢心
3楼-- · 2019-09-17 07:25

The frame has a center property as well as origin and size.

The center is a CGPoint just like origin except it marks the center of the frame instead of the upper lefthand corner.

查看更多
登录 后发表回答