For some reason, the CAT transition will not animate if the animation is added to the subview layer immediately after adding the subview:
transitionView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [transitionView setBackgroundColor:[UIColor redColor]]; [[self contentView] addSubview:transitionView]; CATransition *animation = [CATransition animation]; [animation setDelegate:self]; [animation setType:@"cube"]; [animation setDuration:1.0]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; [animation setSubtype:@"fromRight"]; [[transitionView layer] addAnimation:animation forKey:nil];
But if the animation is added after a short delay:
transitionView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [transitionView setBackgroundColor:[UIColor redColor]]; [[self contentView] addSubview:transitionView]; CATransition *animation = [CATransition animation]; [animation setDelegate:self]; [animation setType:@"cube"]; [animation setDuration:1.0]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; [animation setSubtype:@"fromRight"]; [self performSelector:@selector(animate) withObject:nil afterDelay:0.00001];
-
- (void)animate { CATransition *animation = [CATransition animation]; [animation setDelegate:self]; [animation setType:@"cube"]; [animation setDuration:1.0]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; [animation setSubtype:@"fromRight"]; [[transitionView layer] addAnimation:animation forKey:nil]; }
The transition will work as expected. Is there a reason for this?
source share