如何在iOS

时间:2015-06-23 10:38:23

标签: ios objective-c swift uilabel cgaffinetransform

我使用以下代码来旋转标签文本

 mylabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

这会奏效。但它会在中心轮换UILabel

让我说UILabel的左上角是(10,10)当我旋转它时左上角不应该改变(就像半径或圆圈一样)。

我如何相对于左上角旋转?

请帮帮我

编辑:

这是代码

  var  transA = CGAffineTransformMakeTranslation(mylabel.frame.size.width/2,mylabel.frame.size.height/2);
  var  rotation = CGAffineTransformMakeRotation(CGFloat(M_PI_4));
  var  transB = CGAffineTransformMakeTranslation(-mylabel.frame.size.width/2,-mylabel.frame.size.height/2);


mylabel.transform = CGAffineTransformConcat(CGAffineTransformConcat(transA,rotation),transB);


   var  transform = transA
   mylabel.transform = CGAffineTransformConcat(mylabel.transform, transform)

编辑问题: 如何使我的Label看起来像Circle的半径。即我可以相对于原点360旋转它?

1 个答案:

答案 0 :(得分:4)

你可以做两件事。

<强> A)

您可以更改标签的锚点:

mylabel.layer.anchorPoint = CGPointMake(0,0); // This will set the anchor point to the top left

现在你可以绕这一点旋转。这将改变所有变换的锚点!

<强> B)

你可以这样做:

CGAffineTransform transA = CGAffineTransformMakeTranslation(mylabel.frame.size.width/2,mylabel.frame.size.height/2);
CGAFfineTransfrom rotation = CGAffineTransformMakeRotation(M_PI_2);
CGAffineTransform transB = CGAffineTransformMakeTranslation(-mylabel.frame.size.width/2,-mylabel.frame.size.height/2);
mylabel.transform = CGAffineTransformConcat(CGAffineTransformConcat(transA,rotation),transB);

这不会改变您的锚点,因此不会影响其他变换!

(如果轮换错误,您可能需要针对transA更改transB

<强>更新

如果你想“保持”轮换(如评论中所述),你可以这样做:

CGAffineTransform transform = ... // Put the transform here that you used in A) or B)

mylayer.transfrom = CGAffineTransfromConcat(mylayer.transform,transform);

通过这种方式你只需说'我之前',然后再旋转90度(或45度)

更新2

您的代码应如下所示:

var  transA = CGAffineTransformMakeTranslation(mylabel.frame.size.width/2,mylabel.frame.size.height/2);
var  rotation = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
var  transB = CGAffineTransformMakeTranslation(-mylabel.frame.size.width/2,-mylabel.frame.size.height/2);


var transform = CGAffineTransformConcat(CGAffineTransformConcat(transA,rotation),transB);

mylabel.transform = CGAffineTransformConcat(mylabel.transform, transform)