iPhone界面定位 - 这有什么问题?

时间:2011-06-24 20:37:03

标签: iphone objective-c interface xcode4 orientation

我目前正在使用Xcode 4.0.2创建iPhone应用程序,我希望应用程序在用户转动手机时切换视图。例如:如果用户将iPhone置于其侧面,则iPhone将更改为预制的横向视图。我已经为它创建了代码,但是当我在iOS模拟器中运行它并旋转设备时没有任何反应。我做错了什么?

标题文件:

#import "FlipsideViewController.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
IBOutlet UIView *portraitView;
IBOutlet UIView *landscapeView;

}
@property (nonatomic, retain) UIView *portraitView;

@property (nonatomic, retain) UIView *landscapeView;

@end

实施档案(.m):

#import "MainViewController.h"
#define deg2rad (3.1415926/180.0)

@implementation MainViewController
@synthesize portraitView;
@synthesize landscapeView;

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

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    self.view=landscapeView;
    self.view.transform=CGAffineTransformMakeRotation(deg2rad*(90));
    self.view.bounds=CGRectMake(0.0, 0.0, 480.0, 320.0);
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    self.view=landscapeView;
    self.view.transform=CGAffineTransformMakeRotation(deg2rad*(-90));
    self.view.bounds=CGRectMake(0.0, 0.0, 480.0, 320.0);
} else {
    self.view=portraitView;
    self.view.transform=CGAffineTransformMakeRotation(deg2rad*(0));
    self.view.bounds=CGRectMake(0.0, 0.0, 300.0, 460.0);
}

[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

}

@end

我已经取出了一些不必要的东西,所以.m文件可能看起来有点光秃。我已将IBOutlets landscapeView和portraitView链接到一个名为MainViewController.xib的.xib文件中的2个不同视图

当我构建它时,没有错误,警告或信号。

有更简单的方法可以做到这一点,还是我做错了?有人可以帮忙吗? 提前谢谢!

1 个答案:

答案 0 :(得分:3)

你需要修复你的shouldAutorotateToInterfaceOrientation。返回值将始终与第一个匹配,它看起来应该更像

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