开发者

CGAffineTransformMakeRotation around external point

开发者 https://www.devze.com 2023-04-01 01:33 出处:网络
are there a way for rotate UIImage around external开发者_开发问答 point using CGAffineTranformMAkeRotation?

are there a way for rotate UIImage around external开发者_开发问答 point using CGAffineTranformMAkeRotation? tnx a lot!


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

This works exactly how other "RotateAt()" APIs should work.

The operation represents 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);
}

Just like with CGAffineTransformMakeRotation(), angle is in radians, not degrees.


Set the image view's layer's anchorPoint to something outside of (0,0) and (1,1), ie view.layer.anchorPoint = CGPointMake(2, 2).


I've solved in this way: I put the UIImage that i want to rotate into an other view. The view is biggest than the image view so the center point of the view is the external point Of uiimage view, so i rotate the view....


I just gave this a shot. Something like should give you the result you're looking for...

- (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's 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));
}

Just to explain my reasoning a little...

What we're basically doing is physically shifting the view to the point of rotation, then applying a translation to make it look like it stayed at the original point. Then, if we multiply a rotation by the view's new translation, we essentially rotate the entire view and its coordinate system, so it looks like its rotating around the given point. I hope I explained that well. :| If not, I suggest looking up transformation matrices online maybe you can find better explanations for how they stack there!

0

精彩评论

暂无评论...
验证码 换一张
取 消