【问题标题】:Switching views with Transition effect使用过渡效果切换视图
【发布时间】:2016-01-30 23:34:54
【问题描述】:

我正在尝试为 2 个视图之间的切换添加效果。开关发生在旋转设备时。到目前为止,我可以在从纵向到横向时应用该效果,但反之则不行。

这是我的代码:

-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIInterfaceOrientationPortrait)
{ 
    /* If I run with this block, I have a Exc Bad Access Error and can't run, so I put it 
       under comment

    [UIView transitionWithView:self.view  duration:1.0 
options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        [self.view addSubview:self.portraitView];
                    } completion:NULL];
    */ 

    self.view = self.portraitView;

    //[self dismissModalViewControllerAnimated:YES];

} 
else  if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation == 
UIInterfaceOrientationLandscapeRight) 
{   
    [UIView transitionWithView:self.view  duration:1 
options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        [self.view addSubview:self.landscapeView];
                    } completion:NULL];
   [self dismissModalViewControllerAnimated:NO];
}
}

通过这种方式,我在从纵向传递到横向时获得了预期的效果,但是当我将设备放回纵向时,它不会加载纵向视图,横向视图保持打开状态。 希望有人可以帮助我。

编辑:

我只是用这种方式编辑了我上面的代码:

-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIDeviceOrientationPortrait)
{ 

    if (self.isViewLoaded  && self.view.window)
    {
    NSLog(@"Portrait1");
    [UIView transitionWithView:self.view  duration:1 
    options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        NSLog(@"Portrait2");
                        [self.view.window addSubview:self.portraitView];
                    } completion:NULL];
    [self dismissModalViewControllerAnimated:YES];
    }   
}
else  if (deviceOrientation == UIDeviceOrientationLandscapeLeft || deviceOrientation == 
UIDeviceOrientationLandscapeRight) 
{   

    [UIView transitionWithView:self.view  duration:1 
options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        [self.view addSubview:self.landscapeView];
                    } completion:NULL];
   [self dismissModalViewControllerAnimated:NO];
    NSLog(@"Landscape");
}
}

而且效果很好。但是我有一个大问题,当我从横向切换回纵向时,横向视图仍然在纵向视图上。我怎么能忽略它?

【问题讨论】:

  • 我没有尝试过,但也许这与你混淆了 UIDeviceOrientation 和 UIInterfaceOrientation 的事实有关,并且你在第二个 if 语句中很幸运..我虽然无法解释错误的访问错误;当你调试第一个 if 语句时,你碰巧有一个 NSLog() 吗?
  • 我不确定你在说什么:我在 [self.view addSubview:self.landscapeView] 上遇到错误;线。所以我在上面的行中放了一个 NSLog,我在调试窗口中看到了它。你说的是这个吗?
  • 我编辑了我的代码,你能看一下吗?
  • 我只是在黑暗中对我的 NSLog() 问题进行了一次尝试。我问的原因是我想也许你试图用 %@ 说明符打印 UIDeviceOrientation 值(它是一个枚举,它实际上是一个整数),它只适用于对象,而不是 %d。这(即使用 %@ 打印枚举)会导致 Exc Bad Access 错误。

标签: xcode view effect uiswitch


【解决方案1】:

参考您更新的代码,当您的设备方向变为 UIDeviceOrientationPortrait 时,您错误地将 PortraitView 作为子视图添加到 self.view.window 而不是 self.view。

另外,[selfdismissModalViewControllerAnimated:NO] 背后的目的是什么?您实际上并没有在任何地方展示模态视图控制器,因此它没有任何用途。

【讨论】:

  • 事实上,使用 self.view 我得到了 Exc Bad Access Error,所以我添加了 .window。 [self dismissModalViewControllerAnimated:NO] 是我尝试解决问题的众多镜头之一。我取消了。
  • 确切的错误是:线程 1: EXC_BAD_ACCESS (code=2, address= 0xbf7ffffbc) 左边我得到 43664 __37-[ViewControllerorientationChanged:]_block_invoke_0
猜你喜欢
  • 2018-11-25
  • 1970-01-01
  • 2023-03-29
  • 2020-11-05
  • 2018-02-23
  • 2012-10-30
  • 2012-12-03
  • 2021-02-17
  • 2012-05-17
相关资源
最近更新 更多