注:這種類型的動畫是“開始後不處理” -你不能獲取任何有關物體在動畫中的信息(如當前的位置) 。如果您需要此信息,您會手動使用定時器去調整動畫的X和Y座標
這個需要導入QuartzCore.framework
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
//Creates and returns an CAPropertyAnimation instance for the specified key path.
//parameter:the key path of the property to be animated
theAnimation.duration=1;
theAnimation.repeatCount=2;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:0];
theAnimation.toValue=[NSNumber numberWithFloat:-60];
[view.layer addAnimation:theAnimation forKey:@"animateLayer"];
[pool drain];