如何在iPhone中设置设备方向?

时间:2011-11-07 05:36:39

标签: iphone

我正在做一个支持横向模式和肖像模式的项目,但现在我很惊讶我的项目中没有任何想法。我设置了

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    //return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
    return YES;
}

但没有用,当我按下命令+右箭头向右旋转它旋转到左边但视图和控制器没有改变到那个orentation.Then一些谷歌搜索后我得到这个代码

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
    // do stuff
}

但问题是我不知道如何在//中编写代码。请帮我这样做。 提前致谢。  编辑: 我把这个代码,它正在工作但是当模拟器再次变成肖像时它不会处于默认模式。我的代码是

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    {
        if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
        {
            NSLog(@"Csantos shouldAutorotateToInterfaceOrientation: left or right");
            //
            table.transform = CGAffineTransformIdentity;
            table.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
            table.bounds = CGRectMake(0.0, 0.0, 320, 402);//[self.view setFrame:CGRectMake(0.0, 0.0, 768, 90)];
        }
        else
        {
            table.transform = CGAffineTransformIdentity;
            table.transform = CGAffineTransformMakeRotation(degreesToRadian(360));
            table.bounds = CGRectMake(0.0, 51, 320, 402);
        }
               return YES;
   }*/

2 个答案:

答案 0 :(得分:1)

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        //Handle portrait
    }
    else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
     {
         //Handle landscape
     }
}

这是更好的代码。

答案 1 :(得分:0)

你可以这样做。

if (UIInterfaceOrientationPortrait == interfaceOrientation || UIInterfaceOrientationLandscapeLeft == interfaceOrientation || UIInterfaceOrientationLandscapeRight == interfaceOrientation ||
    UIInterfaceOrientationPortraitUpsideDown == interfaceOrientation) {
    return  YES;
}