使用过渡效果切换视图

时间:2012-03-18 09:52:40

标签: xcode view effect uiswitch

我正在尝试为两个视图之间的切换添加效果。只需旋转设备即可实现切换。到目前为止,我可以在从肖像到风景时应用效果,但不能反过来。

这是我的代码:

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

效果很好。但是我有一个很大的问题,当我从Landscape切换回Portrait时,landscapeView仍保持在portraitView上。我该怎么解雇呢?

1 个答案:

答案 0 :(得分:0)

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

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