Cách sạch hơn là KHÔNG sử dụng UIInterfaceĐối với vòng quay.
Lý do, vì sau đó giao diện hoặc chế độ xem của bạn thực sự quay. Điều này có nghĩa là bạn phải xoay nó lại để hiển thị lại sau khi xem 2 bị xóa.
Để bù lại điều này, tôi đơn giản đăng ký Thông báo UIDeviceOrientation. Subtly khác nhau. View 1 KHÔNG phải hỗ trợ autorotation để làm cho phương thức này hoạt động.Trong viewDidLoad nhập đoạn mã sau:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
Sau đó chỉ cần xác định phương pháp detectOrientation:
-(void) detectOrientation {
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
//load view 2
} else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
// load view 1
}
}
Bây giờ không phải quan điểm của bạn cần hỗ trợ autoratation! Tuy nhiên, bạn sẽ cần phải thực hiện chuyển đổi trên chế độ xem 2 trước khi tải:
-(void) transformView2ToLandscape {
NSInteger rotationDirection;
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
if(currentOrientation == UIDeviceOrientationLandscapeLeft){
rotationDirection = 1;
}else {
rotationDirection = -1;
}
CGRect myFrame = CGRectMake(0, 0, 480, 300);
CGAffineTransform transform = [[self view2] transform];
transform = CGAffineTransformRotate(transform, degreesToRadians(rotationDirection * 90));
[[self view2] setFrame: myFrame];
CGPoint center = CGPointMake(myFrame.size.height/2.0, myFrame.size.width/2.0);
[[self view2] setTransform: transform];
[[self view2] setCenter: center];
}
Thats cách tôi hoán đổi chế độ xem xoay vòng mà không hỗ trợ tự động ghi lại trong chế độ xem của tôi.
Nguồn
2009-05-21 16:24:18
Tôi có cần phân lớp tabBarController ban đầu để thêm phương thức shouldAutorotateToInterfaceOrientation vào nó không? Bộ điều khiển tabBar này đã được thiết lập bằng cách sử dụng IB và một vài dòng mã trong ủy nhiệm ứng dụng. Tôi đã làm theo tất cả các bước, nhưng didRotateFromInterfaceOrientation không được gọi. –
Ok, tôi đã phân lớp tabBarController, thêm shouldAutorotateToInterfaceOrientation và bây giờ mọi thứ hoạt động. Cảm ơn bạn rất nhiều vì đã giúp đỡ của bạn. Trân trọng. –
Tôi không thích sử dụng UITabBarController ở tất cả và thay vào đó sử dụng UIViewController với UITabBar. – mk12