2010-07-31 19 views

Trả lời

18

Bạn có thể thực hiện việc này thông qua IB, để tải ứng dụng bằng chân dung và bố cục ngang hoặc bạn có thể làm theo cách lập trình. Đây là về cách lập trình.

Để nhận thông báo về việc thay đổi định hướng, sử dụng

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

và thêm một chức năng như thế này (lưu ý đây là copypasted từ một dự án và rất nhiều dòng còn lại ra ngoài, bạn sẽ cần phải điều chỉnh chuyển đổi với tình hình cụ thể của bạn)

-(void)orientationChanged 
{ 
    UIDeviceOrientation o = [UIDevice currentDevice].orientation; 

    CGFloat angle = 0; 
    if (o == UIDeviceOrientationLandscapeLeft) angle = M_PI_2; 
    else if (o == UIDeviceOrientationLandscapeRight) angle = -M_PI_2; 
    else if (o == UIDeviceOrientationPortraitUpsideDown) angle = M_PI; 

    [UIView beginAnimations:@"rotate" context:nil]; 
    [UIView setAnimationDuration:0.7]; 
    self.rotateView.transform = CGAffineTransformRotate(
           CGAffineTransformMakeTranslation(
            160.0f-self.rotateView.center.x, 
            240.0f-self.rotateView.center.y 
           ),angle); 
    [UIView commitAnimations]; 
} 

Khi bạn đã hoàn tất, ngăn chặn các thông báo như sau:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] removeObserver:self]; 
+0

Hmm, điều này dường như không làm việc hoàn hảo cho tôi vì nó sẽ không xoay chỉ UIImageView/UIView mà là bên trong một UIView. Thay vào đó, nó xoay toàn bộ UIView không chỉ là chế độ xem phụ. – Joshua

+0

sau đó thực hiện 'self.rotateView' chỉ là chế độ xem phụ bạn muốn xoay. – mvds

+3

bạn nên sử dụng 'M_PI_2' thay vì' M_PI/2.0f' –

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