2010-07-31 43 views

回答

18

您可以通过IB完成此操作,以获得具有纵向和横向布局的应用程序,也可以通过编程方式执行此操作。这是关于程序化的方式。

要获得取向的变更通知,使用

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

,并添加这样的函数(注意,这是从一个项目,很多线路copypasted被排除在外,则需要调整改造您的具体情况)

-(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]; 
} 

当您完成后,停止的通知,像这样:

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

嗯,这似乎并不完美,因为它不会旋转只是在另一个UIView内的UIImageView/UIView。相反,它旋转整个UIView不只是子视图。 – Joshua 2010-07-31 13:29:57

+0

然后使'self.rotateView'成为您想要旋转的子视图。 – mvds 2010-08-02 11:13:18

+3

你应该使用'M_PI_2'而不是'M_PI/2.0f' – 2011-06-30 17:16:37