上一节中已经接触过CABasicAnimation
-(void)cupMoveTo:(SCNVector3) position{
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation"];
animation.toValue=[NSValue valueWithSCNVector3:position];
animation.removedOnCompletion=NO;
animation.duration=2;
animation.fillMode=kCAFillModeForwards;
[cupNode addAnimation:animation forKey:@"move"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
cupNode.position= position;
});
}
这里再介绍一个旋转动画效果
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// NSLog(@"touch the screen");
UITouch *touch=[touches anyObject];
CGPoint point = [touch locationInView:[touch view]];
SCNVector3 planePos=[self worldPositionFromScreenPosition:point];
if (planePos.x*planePos.x+planePos.y*planePos.y+planePos.z*planePos.z>3) {
[shipNode removeFromParentNode];
if (shipNode==nil) {
SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/lamp/lamp.scn"];
shipNode = scene.rootNode.childNodes[0];
shipNode.position = SCNVector3Make(0, 0, -1.5);
}
SCNNode *node1 = [[SCNNode alloc] init];
node1.position = planePos;
[sceneView.scene.rootNode addChildNode:node1];
[node1 addChildNode:shipNode];
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"rotation"];
rotationAnimation.duration = 15;
rotationAnimation.toValue = [NSValue valueWithSCNVector4:SCNVector4Make(0, 1, 0, M_PI * 2)];
rotationAnimation.repeatCount = FLT_MAX;
[node1 addAnimation:moonRotationAnimation forKey:@"rotation around"];
}
}