围绕外部点的CGAffineTransformMakeRotation

时间:2011-08-29 15:48:00

标签: iphone ios core-graphics

有没有办法使用CGAffineTranformMAkeRotation围绕外部点旋转UIImage? tnx很多!

4 个答案:

答案 0 :(得分:18)

这是一个使用与CoreGraphics CGAffineTransform API格式相同格式的函数。

这完全适用于其他“RotateAt()”API应该如何工作。

该操作代表以下规范的等价物:translate(pt.x,pt.y);旋转(角度); 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);
}

CGAffineTransformMakeRotation()一样,角度是弧度,而不是度数。

答案 1 :(得分:2)

将图像视图的图层的anchorPoint设置为(0,0)和(1,1)以外的值,即view.layer.anchorPoint = CGPointMake(2,2)。

答案 2 :(得分:2)

我用这种方式解决了: 我把我要旋转的UIImage放到另一个视图中。 视图比图像视图最大,因此视图的中心点是uiimage视图的外部点,所以我旋转视图....

答案 3 :(得分:1)

我刚给了这个镜头。类似的东西可以给你你正在寻找的结果......

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

只是为了解释一下我的推理......

我们基本上做的是将视图物理地移动到旋转点,然后应用翻译使其看起来像是停留在原始点。然后,如果我们将旋转乘以视图的新平移,我们基本上旋转整个视图及其坐标系,因此看起来它围绕给定点旋转。我希望我解释得那么好。 :|如果没有,我建议在线查找转换矩阵,也许你可以找到更好的解释它们如何叠加在那里!

相关问题