前回のコードを少し見直す。
ベースレイヤーの導入
前回は9つのレイヤーすべてにアニメーション設定をしていた。
for (int i=0; i < 9; i++) { UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"image%02ds.jpg", i+1]]; CALayer* layer = [CALayer layer]; : CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"zPosition"]; theAnimation.fromValue=[NSNumber numberWithFloat:-4000]; theAnimation.toValue=[NSNumber numberWithFloat:1000]; theAnimation.duration=10; theAnimation.repeatCount = 1e100; [layer addAnimation:theAnimation forKey:@"zPosition"]; theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; theAnimation.toValue=[NSNumber numberWithFloat:0.0]; theAnimation.duration=10; theAnimation.repeatCount = 1e100; [layer addAnimation:theAnimation forKey:@"opacity"]; }全部同じ動きをするのだからどうも無駄な感じがする。9つのレイヤーを載せるベースとなるレイヤーを用意し、これをアニメーションしてもよさそうだ。こんな感じ。
CALayer* baseLayer = [CALayer layer]; [self.view.layer addSublayer:baseLayer]; for (int i=0; i < 9; i++) { UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"image%02ds.jpg", i+1]]; CALayer* layer = [CALayer layer]; [baseLayer addSublayer:layer]; : } CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"zPosition"]; theAnimation.fromValue=[NSNumber numberWithFloat:-4000]; theAnimation.toValue=[NSNumber numberWithFloat:1000]; theAnimation.duration=10; theAnimation.repeatCount = 1e100; [baseLayer addAnimation:theAnimation forKey:@"zPosition"]; theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; theAnimation.toValue=[NSNumber numberWithFloat:0.0]; theAnimation.duration=10; theAnimation.repeatCount = 1e100; [baseLayer addAnimation:theAnimation forKey:@"opacity"];動作結果は OK。
Responses
Leave a Response