I am try to play an sound at an specific time in an AnimationGroup how could i do this.
Should i use an NSTimer or an other Timer?
for (int i = 0; i<[bildr count]; i++) {
...
CABasicAnimation *fadeInAnimation;
if([[[bildr objectAtIndex:i] objectForKey:@"animation"] isEqualToString:@"fadeIn"]) {
greenLeafImageView.alpha = 0.0;
fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[fadeInAnimation setToValue:[NSNumber numberWithFloat:1.0]];
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.duration = [[[bildr objectAtIndex:i] objectForKey:@"duration"] floatValue];
fadeInAnimation.beginTime = CACurrentMediaTime() + [[[bildr objectAtIndex:i] objectForKey:@"startTime"] floatValue];
}else{
// is sound -> time it
}
[animArray addObject:fadeInAnimation];
[greenLeafImageView.layer addAnimation:fadeInAnimation forKey:nil];
lastpic=i;
}
float totaltime=20.0;
CAAnimationGroup * group = [CAAnimationGroup animation];
group.animations = animArray;
group.duration = totaltime;
[self.view.layer addAnimation:group forKey:nil];
Short answer, yes, NSTimer
is the path you have to take.
I wrote a class to coordinate sounds with animations. You may review it here on github. This class, MKSoundCoordinatedAnimationLayer
, also allows you to "script" the animation through by passing a NSDictionary
with the relevant image, position, alpha, sound, and timing information, which in turn allows you to configure/save the animation in a plist that you load at run time rather than having to modify code in order to tweak an animation.