2012-08-25 51 views
5

我的影片程序正在創建UIView如何檢測旋轉的程序生成的UIView

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; 
    if (self) { 
     self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5]; 

     UIImageView* closeButton = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"modal_close.png"]]; 

     closeButton.frame = CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09, closeButton.frame.size.width, closeButton.frame.size.height); 

     UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09,20, 20)]; 

     [button addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside]; 

     UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width*.1,self.frame.size.height*.1,self.frame.size.width*.8,self.frame.size.height*.8)]; 

     mainView.backgroundColor = [UIColor whiteColor]; 
     _displayMainView = mainView;   

     [self addSubview:_displayMainView]; 
     [self addSubview:closeButton]; 
     [self addSubview:button]; 

     mainView = nil; 
     closeButton = nil; 
    } 
    return self; 
} 

如何檢測旋轉?這是作爲另一種現有觀點的模式。這是一款僅限iPad的應用程序,如果有問題的話。

+0

你的意思是旋轉的UIView的角度,或設備的方向是什麼? –

+0

看到這個優秀的答案 - http://stackoverflow.com/a/3897243/194544 – beryllium

回答

5

我認爲你有兩個選擇:

  1. 你把一個自定義UIViewController的觀點並重寫shouldAutorotateToInterfaceOrientation:方法返回YES所有你想支持的方向。視圖控制器將自動旋轉並調整內容(您的視圖)的大小。您只需確保擁有正確的autoresizeMask(或者如果使用自動佈局,則會受到限制)。
  2. 您可以直接觀察加速度計對設備方向的更改。然後您可以在需要時調整視圖。

這真的是你應該選擇的第一種方法,如果可以的話。

42

你可以讓設備開始產生旋轉的通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

添加您自己的選擇,作爲一個設備旋轉通知觀察員:

 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil]; 

,然後寫您的通知處理程序

- (void)deviceOrientationDidChange:(NSNotification *)notification { 
    //Obtain current device orientation 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

     //Do my thing 
} 

記得在dealloc方法中刪除觀察者你自定義的UIView被調用(或者你已經完成)並停止生成設備更改通知。

-(void) dealloc{ 
[[NSNotificationCenter defaultCenter] removeObserver: self]; 
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 
} 

這對您的問題有幫助嗎?

+0

不要使用它,因爲它會激活設備的加速度計。在您調用'endGeneratingDeviceOrientationNotifications'之前,加速計將保持活動狀態。但是如果你的視圖是一個單元視圖,它將永遠不會被釋放,直到你釋放表視圖(永遠不可能發生)。您可以使用'如果UIScreen.main.bounds.height> UIScreen.main.bounds.width'。 –

3

我重寫@ clearwater82對雨燕3回答,因爲他的做法是我最終使用的一個:

我在視圖的init方法添加以下代碼(控制器這將是它的viewDidLoad方法):

// Handle rotation 
UIDevice.current.beginGeneratingDeviceOrientationNotifications() 
NotificationCenter.default.addObserver(
    self, 
    selector: #selector(self.orientationChanged(notification:)), 
    name: NSNotification.Name.UIDeviceOrientationDidChange, 
    object: nil 
) 


我然後加入orientationChanged方法我的視圖。 如你願意,你可以重命名這個方法,只記得它在上面的代碼段也重新命名:

// Called when device orientation changes 
@objc func orientationChanged(notification: Notification) { 
    // handle rotation here 
} 


最後當視圖被刪除我加入了deinit方法來去除不必要的通知:

deinit { 
    NotificationCenter.default.removeObserver(self) 
    UIDevice.current.endGeneratingDeviceOrientationNotifications() 
} 

這最後一個代碼段所做的是從通知觀察者中刪除視圖本身,然後停止設備生成其他輪轉通知。 這種方法是我所需要的,但您可能不想停止在刪除視圖時生成通知。

我希望這是有用的,歡呼聲