2012-03-18 126 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