我真的在努力弄清楚如何处理轮换。我已经在这里阅读了很多帖子,但无法将正确的方式添加到横向屏幕上,并且位置正确。
例如,我有一个纵向视图(320x480)并在位置(0,0)处创建一个大按钮。我希望它出现在左上角。
当我将设备顺时针旋转到横向(480x320)时,我希望坐标(0,0)也在左上角 - 但它们不是。我的按钮错误地显示在右上方,按钮上的文字沿着屏幕向下而不是横向/最宽的方向。
我会附上我的(杂乱的)代码,希望它能说明我想要做的事情。
假设我可以使用设备旋转坐标系,以便坐标系在横向模式下变为480x320而左上角为0,0我错了吗?如果我没错...我该如何实现?
#include "ContainerVC.h"
@interface UIApplication (AppDimensions)
+(CGSize) currentSize;
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation;
@end
@implementation UIApplication (AppDimensions)
+(CGSize) currentSize
{
return [UIApplication sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation
{
CGSize size = [UIScreen mainScreen].bounds.size;
UIApplication *application = [UIApplication sharedApplication];
if( UIInterfaceOrientationIsLandscape( orientation ) )
{
size = CGSizeMake(size.height, size.width);
}
if (application.statusBarHidden == NO)
size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
return size;
}
@end
@interface ContainerVC ()
@end
@implementation ContainerVC
int g_MaxPixelHeight;
int g_MaxPixelWidth;
-(void) addMyContent
{
// TEST BUTTON !!
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake( 0, 0, 200, 200 );
[button setBackgroundColor: [UIColor redColor]];
// Configure title(s)
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor colorWithRed:.25 green:.25 blue:.25 alpha:1] forState:UIControlStateNormal];
[button setTitleShadowOffset:CGSizeMake(0, -1)];
[button setFont:[UIFont boldSystemFontOfSize:40]];
[button setTitle: @"TITLE" forState:UIControlStateNormal];
[self.view addSubview:button];
return;
}
-(void) loadView
{
// PDS> Stack overflow says don't call this but I get errors if I don't!!
[super loadView];
}
-(void) viewDidLoad
{
NSLog( @"** VIEW DID LOAD" );
[super viewDidLoad];
g_ContainerVC = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( didRotate: ) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
-(void) viewDidAppear:(BOOL)animated
{
NSLog( @"** VIEW DID APPEAR" );
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
-(void) viewWillDisappear:(BOOL)animated
{
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
-(void) viewWillAppear: (BOOL) animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( orientationChanged: ) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void) viewDidDisappear:(BOOL) animated
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation
{
}
-(void) orientationChanged: (NSNotification *) notification
{
CGSize screenSize = [UIApplication currentSize];
NSLog( @"** orientationChanged ** ScreenSize: %d x %d", (int) screenSize.width, (int) screenSize.height );
g_MaxPixelWidth = screenSize.width;
g_MaxPixelHeight = screenSize.height;
return;
}
-(void) didRotate: (id) sender
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];
if( orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown )
{
orientation = (UIDeviceOrientation)cachedOrientation;
}
if( orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight )
{
if( orientation == UIDeviceOrientationLandscapeLeft )
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
else
if( orientation == UIDeviceOrientationLandscapeRight )
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
CGSize screenSize = [UIApplication currentSize];
g_MaxPixelWidth = screenSize.width;
g_MaxPixelHeight = screenSize.height;
NSLog( @"** didRotate: LANDSCAPE: %d x %d", g_MaxPixelWidth, g_MaxPixelHeight );
}
if( orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown )
{
if( orientation == UIDeviceOrientationPortrait )
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
else
if( orientation == UIDeviceOrientationPortraitUpsideDown )
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
CGSize screenSize = [UIApplication currentSize];
g_MaxPixelWidth = screenSize.width;
g_MaxPixelHeight = screenSize.height;
NSLog( @"** didRotate: PORTRAIT: %d x %d", g_MaxPixelWidth, g_MaxPixelHeight );
}
for (UIView *view in self.view.subviews)
{
[view removeFromSuperview];
}
}
-(void) layoutSubviews
{
NSLog( @"- layoutSubviews" );
}
- (void) viewWillLayoutSubviews
{
NSLog( @"- viewWillLayoutSubviews" );
[self addMyContent];
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
答案 0 :(得分:0)