3 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
您必須加載一個(gè)視圖,然后檢查方向并在需要時(shí)加載另一個(gè)視圖。shouldAutorotateToInterfaceOrientation:如果要旋轉(zhuǎn),則返回“是”來(lái)檢查方向。
我使用導(dǎo)航控制器來(lái)管理過(guò)渡。如果我具有人像視圖并且設(shè)備旋轉(zhuǎn),則我推動(dòng)風(fēng)景視圖,然后在返回肖像時(shí)彈出風(fēng)景視圖。
編輯:
我應(yīng)該在shouldAutorotateToInterfaceOrientation中為所有方向返回YES,但是在應(yīng)用啟動(dòng)時(shí)會(huì)調(diào)用此方法嗎?您是否將視圖推入此功能?
方向常數(shù)不是您查詢的全局變量,而是系統(tǒng)發(fā)送給控制器的消息的一部分。因此,在加載視圖控制器之前,您無(wú)法輕松檢測(cè)方向。相反,您可以硬接線該應(yīng)用程序,使其以特定的方向(通常是縱向)開(kāi)始,然后立即旋轉(zhuǎn)。(請(qǐng)參閱移動(dòng)Safari。它始終以縱向開(kāi)始,然后旋轉(zhuǎn)為橫向。)
這是我用來(lái)交換人像和風(fēng)景視圖的兩種方法。
所有三個(gè)視圖控制器都具有此方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
肖像有:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
[self.nav pushViewController:rightLVC animated:NO];
}
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
[self.nav pushViewController:leftLVC animated:NO];
}
}
每個(gè)景觀控制器具有以下功能:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
[self.nav popViewControllerAnimated:NO];
}
該應(yīng)用程序以縱向啟動(dòng)。如果設(shè)備的方向?yàn)闄M向,則按適當(dāng)?shù)臋M向。當(dāng)設(shè)備旋轉(zhuǎn)回縱向時(shí),它將彈出風(fēng)景。對(duì)于用戶而言,它看起來(lái)像是同一視圖將其自身重組為不同的方向。

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
我稍加修改,以允許在肖像或風(fēng)景中初步裝入筆尖
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
? ? if( !nibNameOrNil )? ? ?nibNameOrNil = [self nibNameRotated:[[UIApplication sharedApplication] statusBarOrientation]];
? ? self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
? ? return self;
}
- (NSString*) nibNameRotated:(UIInterfaceOrientation)orientation
{
? ? if( UIInterfaceOrientationIsLandscape(orientation))? ? ?return [NSString stringWithFormat:@"%@-landscape", NSStringFromClass([self class])];
? ? return [NSString stringWithFormat:@"%@", NSStringFromClass([self class])];
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
? ? NSString *nibname = [self nibNameRotated:toInterfaceOrientation];
? ? [[NSBundle mainBundle] loadNibNamed:nibname owner:self options:nil];
? ? [self viewDidLoad];
}
- 3 回答
- 0 關(guān)注
- 532 瀏覽
添加回答
舉報(bào)