我需要围绕一个固定点旋转 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 行时有效。 如何围绕 View 内的某个点旋转 View ?
最佳答案
以下应该有所帮助。棘手的部分是在添加带有框架的 View 后设置 anchor 然后移动 View 。我通过在没有框架的情况下启动 UIView,然后设置其层的位置、边界和 anchor 来解决这个问题。然后我使用旋转围绕 anchor 旋转。
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"];
如果这不是最好的方法或有错误,请有人纠正我 - 我只知道我目前知道的 - 例如当我应该使用 .或 float 后的 f。
关于ios - 围绕固定点旋转 View - Objective-C ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16707764/