2013-03-19 32 views
8

Tôi đang phát triển ứng dụng iOS có SDK mới nhất.Định hướng ngang nhưng giá trị khung dọc?

Tôi đã đặt Hướng ngang đúng hướng là hướng duy nhất có sẵn và tôi có câu hỏi về chế độ xem Chế độ xem.

Đây là mã của tôi:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(deviceOrientationDidChange:) 
               name:UIDeviceOrientationDidChangeNotification 
               object:nil]; 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
} 

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

- (void)deviceOrientationDidChange:(NSNotification*)notification 
{ 
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    if (orientation == AVCaptureVideoOrientationLandscapeLeft) 
     NSLog(@"Orientation: Landscape Left"); 
    else if (orientation == AVCaptureVideoOrientationLandscapeRight) 
     NSLog(@"Orientation: Landscape Right"); 
    NSLog(@"FRAME: %@", NSStringFromCGRect(self.view.frame)); 
} 

Và đây là nhật ký mà tôi nhận được:

Orientation: Landscape Right 
FRAME: {{0, 0}, {320, 568}} 
Orientation: Landscape Right 
FRAME: {{0, 0}, {320, 568}} 

Câu hỏi của tôi là về {{0, 0}, {320, 568}}:

Tại sao tôi lại nhận được những giá trị?

Tôi nghĩ giá trị chính xác sẽ là {{0, 0}, {568, 320}}.

+0

tôi đoán là một videocontroller độ toàn màn hình của nó bạn đang hiển thị, trong khi ứng dụng vẫn ở chế độ dọc. –

+0

Tôi đã thử nghiệm chương trình bằng trình mô phỏng và nó xuất hiện theo hướng ngang bên phải. Và có, nó là một ứng dụng toàn màn hình. – VansFannel

+0

Bạn không nên nghe những thay đổi định hướng thiết bị. 'UIViewController' có các phương thức thích hợp để nghe các thay đổi định hướng giao diện. Thêm mã đăng nhập của bạn vào các phương pháp thích hợp đó. Hãy thử 'didRotateFromInterfaceOrientation:' hoặc 'viewDidLayoutSubviews'. – rmaddy

Trả lời

3

Tôi tin rằng kiến ​​trúc khung không phản ứng với thay đổi hướng.

EDIT: Giải pháp ban đầu của tôi quá phức tạp đối với nhu cầu của bạn. Nếu bạn đang tìm chính xác hướng hiện tại, thì chỉ cần giải thích chiều rộng của rect là chiều cao nếu bạn đang ở chế độ ngang.

+0

Đó là những gì tôi nghĩ. Cảm ơn câu trả lời của bạn. – VansFannel

+1

Hãy xem câu trả lời cho câu hỏi này: http://stackoverflow.com/questions/2686882/incorrect-frame-window-size-after-re-orientation-in-iphone. Nó tốt hơn để sử dụng giới hạn thay vì khung. – VansFannel

+1

Ah, rất tuyệt! Tôi không tìm thấy câu trả lời khi tôi đang tìm kiếm về vấn đề này. Tôi sẽ quay lại mã của tôi và xem điều này có hiệu quả không. Nó chắc chắn thanh lịch hơn. – drewmm

1

Tôi thêm giải pháp này để hiển thị những gì xảy ra với khung và bị ràng buộc.

Tôi đã thêm ba phương pháp:

- (void) viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
     CGRect bounds = [view bounds]; 
     NSLog(@"FRAME: %@", NSStringFromCGRect(view.frame)); 
     NSLog(@"BOUNDS: %@", NSStringFromCGRect(bounds)); 
} 

- (void)deviceOrientationDidChange:(NSNotification*)notification 
{ 
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    if (orientation == AVCaptureVideoOrientationLandscapeLeft) 
     NSLog(@"1. Orientation: Landscape Left"); 
    else if (orientation == AVCaptureVideoOrientationLandscapeRight) 
     NSLog(@"1. Orientation: Landscape Right"); 
    NSLog(@"1. FRAME: %@", NSStringFromCGRect(self.view.frame)); 
    NSLog(@"1. BOUNDS: %@", NSStringFromCGRect(self.view.bounds)); 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    if (orientation == AVCaptureVideoOrientationLandscapeLeft) 
     NSLog(@"2. Orientation: Landscape Left"); 
    else if (orientation == AVCaptureVideoOrientationLandscapeRight) 
     NSLog(@"2. Orientation: Landscape Right"); 
    NSLog(@"2. FRAME: %@", NSStringFromCGRect(self.view.frame)); 
    NSLog(@"2. BOUNDS: %@", NSStringFromCGRect(self.view.bounds)); 
} 

- (void)viewDidLayoutSubviews 
{ 
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    if (orientation == AVCaptureVideoOrientationLandscapeLeft) 
     NSLog(@"3. Orientation: Landscape Left"); 
    else if (orientation == AVCaptureVideoOrientationLandscapeRight) 
     NSLog(@"3. Orientation: Landscape Right"); 
    NSLog(@"3. FRAME: %@", NSStringFromCGRect(self.view.frame)); 
    NSLog(@"3. BOUNDS: %@", NSStringFromCGRect(self.view.bounds)); 
} 

Và đây là những gì tôi nhận được:

FRAME: {{0, 0}, {320, 568}} 
BOUNDS: {{0, 0}, {320, 568}} 
3. Orientation: Landscape Right 
3. FRAME: {{0, 0}, {320, 568}} 
3. BOUNDS: {{0, 0}, {568, 320}} 
1. Orientation: Landscape Right 
1. FRAME: {{0, 0}, {320, 568}} 
1. BOUNDS: {{0, 0}, {568, 320}} 
1. Orientation: Landscape Right 
1. FRAME: {{0, 0}, {320, 568}} 
1. BOUNDS: {{0, 0}, {568, 320}} 

thay đổi ràng buộc về viewDidLayoutSubviews:.

0

Sử dụng self.view.bounds thay vì self.view.frame

0

Vui lòng thay đổi thay đổi trên viewController bạn

SKScene * scene = [MyScene sceneWithSize:skView.bounds.size]; 

thay thế bởi

SKScene * scene = [MyScene sceneWithSize:CGSizeMake(skView.frame.size.height, skView.frame.size.width)]; 
+0

Hi Soner, những gì bạn chỉnh sửa câu trả lời của tôi? – bhaveshvirani63

Các vấn đề liên quan