gpt4 book ai didi

ios - 如何在IOS中制作月亮绕地球旋转同时自己旋转的CAAnimation效果?

转载 作者:行者123 更新时间:2023-11-28 21:50:52 25 4
gpt4 key购买 nike

我知道在IOS中制作月亮绕地球转的效果很简单。假设月亮是一个 CALayer 对象,只需将这个对象的 anchorPoint 改为地球,它就会围绕地球旋转。但是如何创造同时自转的月亮呢?由于月亮只能有一个 anchor ,看来我不能再让这个 CALayer 对象自行旋转了。你们有什么感想?谢谢。

最佳答案

您可以通过沿着贝塞尔曲线路径设置“月亮”的动画来使“月亮”围绕一个点旋转,同时设置旋转变换的动画。这是一个简单的例子,

@interface ViewController ()
@property (strong,nonatomic) UIButton *moon;
@property (strong,nonatomic) UIBezierPath *circlePath;
@end

@implementation ViewController

-(void)viewDidLoad {
self.moon = [UIButton buttonWithType:UIButtonTypeInfoDark];
[self.moon addTarget:self action:@selector(clickedCircleButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.moon];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGRect circleRect = CGRectMake(60,100,200,200);
self.circlePath = [UIBezierPath bezierPathWithOvalInRect:circleRect];
self.moon.center = CGPointMake(circleRect.origin.x + circleRect.size.width, circleRect.origin.y + circleRect.size.height/2.0);
}

- (void)clickedCircleButton:(UIButton *)sender {

CAKeyframeAnimation *orbit = [CAKeyframeAnimation animationWithKeyPath:@"position"];
orbit.path = self.circlePath.CGPath;
orbit.calculationMode = kCAAnimationPaced;
orbit.duration = 4.0;
orbit.repeatCount = CGFLOAT_MAX;
[self.moon.layer addAnimation:orbit forKey:@"circleAnimation"];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
fullRotation.fromValue = 0;
fullRotation.byValue = @(2.0*M_PI);
fullRotation.duration = 4.0;
fullRotation.repeatCount = CGFLOAT_MAX;
[self.moon.layer addAnimation:fullRotation forKey:@"Rotate"];
}

这些特定值将导致“月亮”像地球的月亮一样保持朝向中心的同一面。

关于ios - 如何在IOS中制作月亮绕地球旋转同时自己旋转的CAAnimation效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28399892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com