【发布时间】: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