I want to use Core Animation to simulate flip clock animations in a Mac application. I currently have three CALayer representing the upper and lower half of a digit, and the third is a flip animation (the solution found in the following article: Creating an iPad flip clock with Core Animation .
The animation of the flip layer is divided into two stages: flipping from the top to the middle of the number, and then from the middle to the bottom. To do this, I use the delegate function, which is called whenever the animation ends:
- (void)animationDidStop:(CAAnimation *)oldAnimation finished:(BOOL)flag
{
int digitIndex = [[oldAnimation valueForKey:@"digit"] intValue];
int currentValue = [[oldAnimation valueForKey:@"value"] intValue];
NSMutableArray *digit = [digits objectAtIndex:digitIndex];
CALayer *flipLayer = [digit objectAtIndex:tickerFlip];
CALayer *bottomLayer = [digit objectAtIndex:tickerBottom];
if([[oldAnimation valueForKey:@"state"] isEqual:@"top"] && flag) {
NSLog(@"Top animation finished");
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
flipLayer.contents = [bottomImages objectAtIndex:currentValue];
flipLayer.anchorPoint = CGPointMake(0.0, 1.0);
flipLayer.hidden = NO;
[CATransaction commit];
CABasicAnimation *anim = [self generateAnimationForState:@"bottom"];
[anim setValue:[NSString stringWithFormat:@"%d", digitIndex] forKey:@"digit"];
[anim setValue:[NSString stringWithFormat:@"%d", currentValue] forKey:@"value"];
[flipLayer addAnimation:anim forKey:nil];
} else if([[oldAnimation valueForKey:@"state"] isEqual:@"bottom"] && flag) {
NSLog(@"Bottom animation finished");
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
bottomLayer.contents = [bottomImages objectAtIndex:currentValue];
flipLayer.hidden = YES;
flipLayer.anchorPoint = CGPointMake(0.0, 0.0);
[CATransaction commit];
}
}
This delegate function uses a helper function that generates a transform for the flip layer depending on its state:
- (CABasicAnimation *)generateAnimationForState:(NSString *)state
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 0.15;
anim.repeatCount = 1;
if([state isEqualToString:@"top"])
{
anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 1, 0, 0)];
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI/2, 1, 0, 0)];
}
else
{
anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-M_PI/2, 1, 0, 0)];
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 1, 0, 0)];
}
anim.delegate = self;
anim.removedOnCompletion = NO;
[anim setValue:state forKey:@"state"];
return anim;
}
, . , - "" "". , - , .
, . Core Animation . - ?