CGAffineTransformMakeRotation around an outside point

Is there a way to rotate a UIImage around an outside point using CGAffineTranformMAkeRotation? tnx a lot!

+5
source share
4 answers

Here is a function that uses the same format as the CoreGraphics CGAffineTransform API format.

This works exactly the way other RotateAt () APIs should work.

The operation is the equivalent of the following specification: translate (pt.x, pt.y); rotate (angle); translate (-pt.x, -pt.y);

CGAffineTransform CGAffineTransformMakeRotationAt(CGFloat angle, CGPoint pt){
    const CGFloat fx = pt.x, fy = pt.y, fcos = cos(angle), fsin = sin(angle);
    return CGAffineTransformMake(fcos, fsin, -fsin, fcos, fx - fx * fcos + fy * fsin, fy - fx * fsin - fy * fcos);
}

As with CGAffineTransformMakeRotation(), the angle is in radians, not degrees.

+18
source

anchorPoint - (0,0) (1,1), .. view.layer.anchorPoint = CGPointMake (2, 2).

+2

: UIImage, . , , iiimage, ....

+2

. - , ...

- (void)rotateView:(UIView *)view aroundPoint:(CGPoint)point withAngle:(double)angle{
    //save original view center
    CGPoint originalCenter = view.center; 

    //set center of view to center of rotation
    [view setCenter:point];

    //apply a translation to bring the view back to its original point
    view.transform = CGAffineTransformMakeTranslation(originalCenter.x, originalCenter.y);

    //multiply the view existing rotation matrix (the translation) by a rotation and apply it to the view
    //thereby making it rotate around the external point
    view.transform = CGAffineTransformConcat(view.transform, CGAffineTransformMakeRotation(angle));
}

, ...

What we basically do is physically shift the view to the rotation point, and then apply the translation so that it looks like it remains at the starting point. Then, if we multiply the rotation by a new new translation, we essentially rotate the whole view and its coordinate system, so it looks like it rotates around a given point. I hope I explained it well.: | If not, I suggest looking for transformational matrices online, perhaps you can find more detailed explanations of how they add up there!

+1
source

All Articles