我知道在IOS中创建月球绕地球转的效果很简单。假设月球是一个 CALayer 对象,只需将该对象的 anchorPoint 更改为地球,它就会动画围绕地球旋转。但是如何创造同时自转的月亮呢?由于月亮只能有一个锚点,看来我不能再让这个CALayer对象自行旋转了。你们有什么感想?谢谢。
问问题
710 次
2 回答
1
使用两层。
一个是从地球到月球的无形“手臂”。它围绕其锚点(即地球中心)进行旋转变换。这导致位于“手臂”末端的月球围绕地球旋转。
另一个是月亮。它是“手臂”的一个子层,位于手臂末端。如果您希望它独立旋转,请将其围绕其锚点旋转,该锚点是它自己的中心。
(但是请注意,真正的月亮不会这样做。对于真正的月亮,“手臂”就足够了,因为真正的月亮与它自己围绕地球的公转同步 - 所以我们总是看到同一张脸月亮的。)
于 2015-02-09T02:58:45.480 回答
1
您可以通过沿贝塞尔路径为“月亮”设置动画,同时为旋转变换设置动画,使“月亮”围绕一个点旋转。这是一个简单的例子,
@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"];
}
这些特定的值将导致“月亮”像地球的月亮一样保持朝向中心的同一面。
于 2015-02-09T02:25:54.450 回答