簡體   English   中英

自定義自動旋轉iPhone SDK

[英]Custom autorotation iPhone SDK

我想實現自己的自動旋轉界面的方式:代碼:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 NSLog(@"should!");
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:0.4];
 if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
  trash.transform = CGAffineTransformMakeRotation(M_PI*0.5);
 }
 else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
  trash.transform = CGAffineTransformMakeRotation(-M_PI*0.5);
 }
 else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
  trash.transform = CGAffineTransformIdentity;//CGAffineTransformMakeRotation(M_PI);
 }
 else {
  trash.transform = CGAffineTransformIdentity;

 }
 [UIView commitAnimations];
 return NO;
}

如您所見,我有一個UIView(實際上是UIImageView)-廢紙and,並且在更改接口方向時使用CGAffineTransformMakeRotation對其進行旋轉。 它的工作原理很好,但當界面方向為默認縱向時,永遠不會調用該方法(上下顛倒有效)。 因此,垃圾桶將旋轉到以前的方向,而不是默認位置。 我有點擔心方法上方的注釋(創建UIViewController時會自動添加),上面寫着“允許默認縱向以外的其他方向”。

當我返回YES而不是NO時,當然會為每個方向調用該方法,但是我不想為整個界面設置動畫,而只是為某些元素設置動畫。

請幫助。

-shouldAutorotateToInterfaceOrientation:並不意味着您要執行任何操作,而是返回YES或NO。 這不是告訴您iPhone已經旋轉或即將旋轉的電話。 這是您的viewController詢問的一個問題,以查看它是否允許除當前方向以外的其他方向。

如果您沒有視圖控制器或子類,則不需要此調用。 無論哪種方式,您都返回YES或NO,然后保留該值。

其余的代碼,可以在輕按按鈕或從加速度計檢測到運動等時使用。

(通常,視圖控制器會幫您一個忙,並為您進行旋轉,而shouldAutorotateToInterfaceOrientation只是您的批准。如果您沒有視圖控制器而工作或想自己旋轉一些元素,那么您只需在代碼中找到合適的時機即可旋轉...)

如果只想知道設備的當前方向( 可能與UI的旋轉方向不同 ),請使用beginGeneratingDeviceOrientationNotificationsUIDevice orientation方法。

如果要在旋轉時更改接口, didRotateFromInterfaceOrientationUIViewController實現didRotateFromInterfaceOrientation

我只想在用戶更改設備方向時旋轉子視圖,那么您會要求什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM