Fade-in / fade-out during an interface rotation
When my iPhone interface rotates, I would like to do a fade-in/fade-out for a specific UIView of a UIViewController... Like...
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
theView.alpha = 0;
[UIView commitAnimations];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[UIView beginAnimations:nil context:nil];
[UIView setAni开发者_如何学编程mationDuration:0.3];
theView.alpha = 1;
[UIView commitAnimations];
}
But the animation doesn't finish before the rotation start (we can see the view starting to self-resize)...
Is there a way to delay rotation start ?
"duration" is the duration of the rotating animation, right ?
I found that running the current run loop for the same amount of time as the preceding animation, did in fact delay the rotation.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[UIView animateWithDuration:0.25 animations:^{
theview.alpha = 0.0;
}];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}
Your problem stems from the fact that by the time willRotateToInterfaceOrientation: is called, the view being rotated already has its orientation property set and the animation block handling the rotation is also ready to run on a separate thread. From the documentation:
This method is called from within the animation block used to rotate the view. You can override this method and use it to configure additional animations that should occur during the view rotation.
I would suggest overriding the shouldAutorotateToInterfaceOrientation: method to fire your animation before returning YES for supported orientations:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (interfaceOrientation == (UIDeviceOrientationPortrait || UIDeviceOrientationPortraitUpsideDown) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
theView.alpha = 0;
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
theView.alpha = 1;
[UIView commitAnimations];
}
return YES;
}
This should ensure the animation runs before you ever set the orientation of UIViewController and fire the rotation animation. You may have to add a slight delay to get the effect your are looking for depending on the device hardware speed.
精彩评论