旋转最右下角的控件

时间:2012-03-23 09:48:04

标签: ios ios5 ios4

嗨我想在右下角旋转一个控件(就像钟针一样)。我可以使用以下代码执行此操作:

self.label.layer.anchorPoint = CGPointMake(1.0, 1.0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
self.label.transform = CGAffineTransformMakeRotation(M_PI/2);
[UIView commitAnimations]; 

但问题是我的标签在x轴上向左移动一点然后旋转。我没有得到什么问题..请帮忙。

类似文章:iPhone, How do I rotate an UIImageView around a point using Core Animation and CATransform3D?

2 个答案:

答案 0 :(得分:0)

在这种情况下我做的是在动画块之前移动标签。例如,如果要将锚点更改为CGPoint 1.0,1.0,则只需在动画之前将变换应用于该标签。如果您计划执行多个变换,也可以将viewDidLoad中的参考点保存到原始位置。 *代码实际上没有编译,但这应该给你一个好主意。

- (void)viewDidLoad 
{
    // CGPoint originalLabelsOrigin is declared as a property
    originalLabelsOrigin = self.label.frame.origin;
}  

self.label.layer.anchorPoint = CGPointMake(1.0, 1.0);
self.label.layer.frame = CGRectMake(originalLabelsOrigin.x + self.label.frame.size.width, originalLabelsOrigin.y + self.label.frame.size.height, self.label.frame.size.width, self.label.frame.size.height);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
self.label.transform = CGAffineTransformMakeRotation(M_PI/2);
[UIView commitAnimations]; 

答案 1 :(得分:0)

所以最后我所要求的可以通过遵循船长建议的代码来实现:

- (void)viewDidLoad 
{
// CGPoint originalLabelsOrigin is declared as a property
originalLabelsOrigin = self.label.frame.origin;
}  

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.label.layer.anchorPoint = CGPointMake(1.0, 1.0);
self.label.layer.frame = CGRectMake(originalLabelsOrigin.x, originalLabelsOrigin.y,         self.label.frame.size.width, self.label.frame.size.height);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
self.label.transform = CGAffineTransformMakeRotation(M_PI/2);
[UIView commitAnimations]; 
} 

仍然不太确定原因,但它确实有效。