ios - 在不旋转 View 本身的情况下围绕中心点旋转 UIView

标签 ios objective-c uiview quartz-core

我想在不旋转 UIView 本身的情况下围绕中心点旋转 UIView。所以更像是摩天轮的汽车(始终保持直立),而不是时钟的指针。

我围绕中心点旋转 UIViews 的时候,我使用图层位置/anchorPoint/transform 来实现效果。但这显然会改变 View 本身的方向。

有什么帮助吗?

凯勒

最佳答案

这在 Core Animation 中很容易做到。我在这个例子中使用了一些非常无聊的静态值,所以显然您需要进行一些修改。但这将向您展示如何沿着圆形 UIBezierPath 移动 View 。

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"];

以及生成 View 初始中心的基本函数。

- (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;
}

关于ios - 在不旋转 View 本身的情况下围绕中心点旋转 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18112007/

相关文章:

javascript - 如何根据index.html页面中的url使用ng-if?

ios - 如何手动设置在应用程序委托(delegate)中显示哪个 Storyboard View

ios - 像 NSFetchedController 在 FMDB 的 Core Data 中使用的那样缓存结果

ios - 如何从另一个 ViewController 更改背景颜色?

ios - AudioKit AKNodeRecorder 创建空的音频文件

ios - 以不同方式突出显示 TextField 中的某些文本

objective-c - 下划线的合成为什么不起作用?

iphone - 如何创建径向 UIView?

ios - 生成命令失败 : CompileAssetCatalog - Cordova for iOS

iOS 等同于 onRestart()