2012-03-18 123 views
0

我试图将效果添加到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]; 
} 
} 

这样从纵向传递到风景时,我收到预期的效果,但是当我把设备返回到肖像它不会加载portraitView的landscapeView遗体上。 希望有人能帮助我。

编辑:

我刚编辑我上面的代码是这样的:

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

而影响正常工作。但是我有一个大问题,那就是当我从横向切换回纵向时,横向视图保持在纵向视图上。我怎么能解雇它?

+0

我还没有尝试过,但也许它与事实有关,你混淆了UIDeviceOrientation和UIInterfaceOrientation,并且你在第二条if语句中幸运..我无法解释但访问错误,但;当你在调试第一个if语句时,你碰巧有NSLog()吗? – Aky 2012-03-18 10:20:44

+0

我不确定要理解你在说什么:我在[self.view addSubview:self.landscapeView]上出错了;线。所以我把NSLog放在上面,并在调试窗口中看到它。这是你在说什么吗? – Enzoses 2012-03-18 11:48:20

+0

我编辑了我的代码,可以请看一下吗? – Enzoses 2012-03-18 12:14:02

回答

0

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

另外,[self dismissModalViewControllerAnimated:NO]背后的目的是什么?你实际上并没有在任何地方展示模态视图控制器,所以它不起任何作用。

+0

事实是,在self.view中,我得到了Exc Bad Access Error,所以我添加了.window。 [self dismissModalViewControllerAnimated:NO]是我尝试修复问题的很多镜头之一。我取消了它。 – Enzoses 2012-03-24 13:29:38

+0

确切的错误是:线程1:EXC_BAD_ACCESS(code = 2,address = 0xbf7ffffbc),在左边我得到了43664 __37- [ViewController orientationChanged:] _block_invoke_0 – Enzoses 2012-03-24 13:42:47