首页 文章

界面旋转期间的淡入/淡出

提问于
浏览
5

当我的iPhone界面旋转时,我想为UIViewController的特定UIView做淡入/淡出......就像......

- (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 setAnimationDuration:0.3];
    theView.alpha = 1;
    [UIView commitAnimations];  
}

但动画在旋转开始之前没有完成(我们可以看到视图开始自我调整大小)......

有没有办法延迟旋转开始?

“持续时间”是旋转动画的持续时间,对吧?

2 回答

  • 7

    我发现当前运行循环运行的时间与前一个动画相同,确实延迟了旋转 .

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        [UIView animateWithDuration:0.25 animations:^{
            theview.alpha = 0.0;
        }];
    
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
    }
    
  • 0

    你的问题源于这样一个事实:当调用willRotateToInterfaceOrientation:时,被旋转的视图已经设置了它的orientation属性,并且处理旋转的动画块也准备好在一个单独的线程上运行 . 来自the documentation

    从用于旋转视图的动画块中调用此方法 . 您可以覆盖此方法并使用它来配置在视图旋转期间应发生的其他动画 .

    我建议覆盖shouldAutorotateToInterfaceOrientation:方法来触发动画,然后返回YES以获得支持的方向:

    - (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;
    }
    

    这应确保在设置UIViewController的方向并触发旋转动画之前运行动画 . 您可能需要添加一点延迟才能获得所需的效果,具体取决于设备的硬件速度 .

相关问题