围绕固定点旋转视图 - 目标C.

时间:2013-05-23 07:13:40

标签: ios objective-c ipad

我需要围绕固定点旋转UIView。

bubbleTail = [[BubbleTail alloc] initWithFrame:bubbleTailFrame];
[self addSubview:bubbleTail];
bubbleTail.layer.anchorPoint = triangle_top_left;
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 180 / 180.0);
bubbleTail.transform = transform;

仅当我评论.anchorPoint行时才有效。 如何旋转视图内某点的视图?

2 个答案:

答案 0 :(得分:2)

以下内容应该有所帮助。棘手的部分是在添加带有框架的视图后设置锚点然后移动视图。我通过在没有框架的情况下启动UIView,然后设置其图层的位置,边界和锚点来解决这个问题。然后我使用旋转围绕锚点旋转。

CABasicAnimation *rotation;
rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotation.fromValue = [NSNumber numberWithFloat:0];
rotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
rotation.duration = 60.0;
rotation.repeatCount = HUGE_VALF;

UIView *myview = [[UIView alloc] init];
[self.view addSubview:myview];
[myview setBackgroundColor:[UIColor orangeColor]]; 
[myview.layer setPosition:CGPointMake(10, 10)];
[myview.layer setBounds:CGRectMake(0, 0, 100, 100)];
[myview.layer setAnchorPoint:CGPointMake(0, 0)];
[myview.layer addAnimation:rotation forKey:@"myAnimation"];

如果这不是最好的方法或有错误,有人请纠正我 - 我只知道我目前所知道的 - 例如什么时候我应该用。或浮动后的f。

答案 1 :(得分:0)

似乎CGAffineTransformMakeRotation围绕视图的center旋转了它?

假设您在视图中有点P (px,py),您希望视图围绕该点旋转 我们A(ax,ay) = P - center =(px - centerx,py - centery)。
你有角度alpha

然后你可以使用- A制作一个翻译矩阵,然后将其与旋转矩阵乘以alpha,然后将其与翻译矩阵乘以+ A

您需要的功能包括:CGAffineTransformMakeTranslationCGAffineTransformMakeRotationCGAffineTransformConcat

P.S。不确定这是否会起作用(也不确定如何表示视图rect,也许它首先假设绕中心旋转是错误的,然后你应该假设A = P),但这就是用仿射变换完成的。