代码之家  ›  专栏  ›  技术社区  ›  Damien Debin

在接口旋转期间淡入/淡出

  •  5
  • Damien Debin  · 技术社区  · 15 年前

    当我的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 回复  |  直到 13 年前
        1
  •  7
  •   Måns Severin    13 年前

    我发现运行当前运行循环的时间与前面的动画相同,实际上延迟了旋转。

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

    您的问题源于这样一个事实:当调用willRotateToInterfaceOrientation:时,正在旋转的视图已经设置了其“方向”属性,并且处理旋转的动画块也准备在单独的线程上运行。从 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的方向和激发旋转动画之前动画运行。根据设备硬件速度的不同,您可能需要添加一点延迟才能获得所需的效果。