简体   繁体   中英

rotate UIImageView around an arbitrary point

I have a UIImageView that I rotate around its center:

imageHorizon.layer.anchorPoint = CGPointMake(0.5, 0.5);
imageHorizon.transform = CGAffineTransformRotate(imageHorizon.transform, angleToRotate*(CGFloat)(M_PI/180));

Sometimes I also move this image to the left or right and then rotate again. I would like to keep the rotation center all the time on the same point (which is actually the center of the super view). How can I do that ?

cheers,

self.imgView.layer.anchorPoint = CGPointMake(0.0,1.0);
self.imgView.layer.position = CGPointMake(100,200.0);
CGAffineTransform cgaRotateHr = CGAffineTransformMakeRotation(-(3.141/4));
[self.imgView setTransform:cgaRotateHr];

This is an older question, but the other solutions did not work well for me, so I came up with another solution:

Rotating an image is essentially just a normal rotation with a translation applied, ensuring that the point you want to rotate around is still in the same spot after the rotation. To do this, calculate the position's CGPoint in your image before the rotation, get the position after the rotation, and apply the difference as a translation on the image, "snapping" it into the right position. Here is the code that I've been using:

Keep in mind that the translation should be applied via CGAffineTransform, not moving the .center, because the translation will need to be relative to the rotation, and CGAffineTransformTranslate() takes care of that.

// Note: self is the superview of _imageView

// Get the rotation point
CGPoint rotationPointInSelf = self.center; // or whatever point you want to rotate around
CGPoint rotationPointInImage = [_imageView convertPoint:rotationPointInSelf fromView:self];

// Rotate the image
_imageView.transform = CGAffineTransformRotate(_imageView.transform, angle);

// Get the new location of the rotation point
CGPoint newRotationPointInImage = [_imageView convertPoint:rotationPointInSelf fromView:self];

// Calculate the difference between the point's old position and its new one
CGPoint translation = CGPointMake(rotationPointInImage.x - newRotationPointInImage.x, rotationPointInImage.y - newRotationPointInImage.y);

// Move the image so the point is back in it's old location
_imageView.transform = CGAffineTransformTranslate(_imageView.transform, -translation.x, -translation.y);

You can make the image a subview of another view and then rotate the superview to get that effect. Another approach is to set the anchorPoint property as described in the docs .

I'm using this code to rotate around the point (0,0). Maybe it help you figure out how to active what you want.

    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height;

    CGRect frame_smallView = CGRectMake(-width, -height, width, height);
    UIView *smallView = [[UIView alloc] initWithFrame:frame_smallView];
    smallView.backgroundColor = darkGrayColor;

    // Select x and y between 0.0-1.0. 
    // The default is (0.5f,0.5f) that is the center of the layer
    // (1.0f,1.0f) is the right bottom corner
    smallView.layer.anchorPoint = CGPointMake(1.0f, 1.0f);

    // Rotate around this point
    smallView.layer.position = CGPointMake(0, 0);

    [self.view insertSubview:smallView belowSubview:self.navBar];

    [UIView animateWithDuration:1
                     animations:^{
                         smallView.transform = CGAffineTransformMakeRotation(M_PI);
                     }
                     completion:^(BOOL finished){
                         [self.navigationController popViewControllerAnimated:NO];
                     }];

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