围绕中心点旋转UIView而不旋转视图本身

Kel*_*ler 5 objective-c uiview ios quartz-core

我想在不旋转UIView本身的情况下围绕中心点旋转UIView.所以更像是摩天轮的车(总是保持直立),而不是钟表的手.

我围绕中心点旋转UIViews的时间,我使用了图层位置/ anchorPoint /变换来完成效果.但这显然会改变视图本身的方向.

有帮助吗?

凯勒

Mic*_*lum 9

在Core Animation中这很容易做到.我在这个例子中使用了一些相当无聊的静态值,所以很明显你会想做一些修改.但是这将向您展示如何沿着圆形UIBezierPath移动视图.

UIView *view = [UIView new];
[view setBackgroundColor:[UIColor redColor]];
[view setBounds:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
[view setCenter:[self pointAroundCircumferenceFromCenter:self.view.center withRadius:140.0f andAngle:0.0f]];
[self.view addSubview:view];

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(CGRectGetMidX(self.view.frame) - 140.0f, CGRectGetMidY(self.view.frame) - 140.0f, 280.0f, 280.0f)];

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 5.0;

pathAnimation.path = [path CGPath];

[view.layer addAnimation:pathAnimation forKey:@"curveAnimation"];
Run Code Online (Sandbox Code Playgroud)

并且是生成视图初始中心的基本功能.

- (CGPoint)pointAroundCircumferenceFromCenter:(CGPoint)center withRadius:(CGFloat)radius andAngle:(CGFloat)theta
{
    CGPoint point = CGPointZero;
    point.x = center.x + radius * cosf(theta);
    point.y = center.y + radius * sinf(theta);

    return point;
}
Run Code Online (Sandbox Code Playgroud)