2017-05-09 92 views
0

我已经尝试了很多来自这个网站和其他人的代码......但似乎没有任何工作适合我。这里是我的代码...如何改变按钮点击屏幕的方向?

- (IBAction)orientation:(id)sender { 
    self.currentDeviceOrientation = [[UIDevice currentDevice] orientation]; 
    int cur = self.currentDeviceOrientation; 
    int port = UIInterfaceOrientationPortrait; 
    int landl = UIInterfaceOrientationLandscapeLeft; 
    int landr = UIInterfaceOrientationLandscapeRight; 

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) 
    { 
     NSNumber *value= [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft]; 
     [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"orientation"]; 
     [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
     [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES]; 
    } 
} 

上的一个按钮的点击,我想在屏幕从当前方向即发生改变,如果屏幕处于纵向模式时,应该改为横向和副诗。

+0

的[我如何编程在iOS7设置设备的方向?(可能的复制http://stackoverflow.com/questions/20987249/how-do-i-programmatically-set-device-orientation-in- ios7) –

回答

0

尝试此操作并根据需要进行更改。

- (IBAction)orientation:(id)sender { 

     if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){ 

     NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; 
     [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
     NSLog(@"landscape left"); 

     } 

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){ 

     NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight]; 
     [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
     NSLog(@"landscape right"); 

     } 

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){ 

     NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; 
     [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
     NSLog(@"portrait"); 

     } 


} 
+0

这有效,但问题是,这不应该在如果情况下,因为这不会等待第二个按钮单击,即一次点击它不断改变方向.....所以我的问题得到解决当我将它们保存在开关盒中时....... –

0

您需要根据需要设置方向。

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; 
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];