2010-06-09 36 views
11

Tôi đang làm việc trên ứng dụng iPhone dựa trên UITabBarController và UIViewControllers cho mỗi trang. Ứng dụng cần để chạy ở chế độ dọc mà thôi, như vậy mỗi điểm điều khiển + ứng dụng đại biểu đi với dòng mã này:Phát hiện xoay vòng theo phong cảnh thủ công

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

Có một quan điểm điều khiển nơi tôi muốn bật lên một UIImageView khi iPhone là rotaed để landscapeleft. Thiết kế của hình ảnh trông ngang, mặc dù chiều rộng và chiều cao là 320x460 (do đó ảnh chân dung của nó).

Làm cách nào để/tôi có thể phát hiện loại xoay vòng theo cách thủ công, chỉ trong bộ điều khiển chế độ xem cụ thể này, với chế độ xoay tự động trên toàn bộ chế độ xem?

Thomas

UPDATE:

Cảm ơn!

tôi thêm listener này trong viewDidLoad:

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

các didRotate trông như thế này:

- (void) didRotate:(NSNotification *)notification 

    { 
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

     if (orientation == UIDeviceOrientationLandscapeLeft) 
     { 
      //your code here 

     } 
    } 

Trả lời

20

tôi cần rằng trong một dự án cũ - hy vọng nó vẫn hoạt động ...

1) Đăng ký thông báo:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(detectOrientation) 
              name:UIDeviceOrientationDidChangeNotification 
              object:nil]; 

2) Sau đó, bạn có thể kiểm tra vòng xoay khi thay đổi:

-(void) detectOrientation { 
    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
     ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) { 
     [self doLandscapeThings]; 
    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) { 
     [self doPortraitThings]; 
    } 
} 

Hy vọng điều đó sẽ hữu ích!

+3

Cảm ơn! Tôi đã thêm người nghe này trong chế độ xemDidLoad: [[NSNotificationCenter defaultCenter] addObserver: tự chọn: @selector (didRotate:) tên: UIDeviceOrientationDidChangeNotification object: nil]; các didRotate trông như thế này: - (void) didRotate: (NSNotification *) thông báo {\t \t UIDeviceOrientation định hướng = [[UIDevice currentDevice] hướng]; \t \t if (định hướng == UIDeviceOrientationLandscapeLeft) \t { \t \t // mã của bạn ở đây \t}} –

+1

cải thiện nhỏ, sử dụng UIDeviceOrientationDidChangeNotification liên tục chứ không phải là giá trị chuỗi của nó (đó là @ "UIDeviceOrientationDidChangeNotification") –

+0

Đừng quên tự hủy đăng ký với tư cách người nghe cho sự kiện ... – aroth

2

mã tốt hơn để Comic Sans câu trả lời sẽ là dưới đây .. Mã của ông sẽ phải lúc nào cũng bắn một cách chính xác (chỉ có 80% thời gian trong thử nghiệm của tôi)

-(void) detectOrientation { 
    if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) { 
     [self setupForLandscape]; 
    } else if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { 
     [self setupForPortrait]; 
    } 
} 
Các vấn đề liên quan