iPhone 界面方向 - 这有什么问题吗?

发布于 2024-11-17 05:29:27 字数 2085 浏览 3 评论 0原文

我目前正在使用 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 个不同视图。

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

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

I am currently creating an iPhone app using Xcode 4.0.2, I want the app to switch views when the user turns their phone. For example: If a user turns their iPhone on it's side, the iPhone would change to a pre-made landscape view. I have created the code for it, but when I run it in iOS Simulator and rotate the device nothing happens. What am I doing wrong?

Header File:

#import "FlipsideViewController.h"

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

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

@property (nonatomic, retain) UIView *landscapeView;

@end

Implementation File (.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

I have taken out some things that are not necessary, so the .m file may look a little bit bare. I have linked the IBOutlets landscapeView and portraitView to 2 different views within one .xib file called MainViewController.xib

When I build it, there are no errors, warnings, or signals.

Is there an easier way to do this or am I just doing it wrong? Could somebody help?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2024-11-24 05:29:27

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

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

You need to fix your shouldAutorotateToInterfaceOrientation for one. The return value will always match up with the first, it should look more like

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait)
        || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        ||(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文