简体   繁体   中英

How can I rotate and move a UIView at the same time?

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.

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.

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);

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