iPhone 纵向/横向和坐标系
Posted
技术标签:
【中文标题】iPhone 纵向/横向和坐标系【英文标题】:iPhone Portrait/Landscape and Coordinate System 【发布时间】:2013-10-07 19:23:42 【问题描述】:我真的很难弄清楚如何处理旋转。我在这里阅读了许多帖子,但无法以正确的方式将项目添加到横向屏幕并位于正确的位置。
例如,我有一个纵向视图 (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
【问题讨论】:
仅供参考:layoutSubviews
应始终致电 [super layoutSubviews]
。
@progrmr:谢谢。这能解决我的问题吗?
【参考方案1】:
我认为这可能是答案 - 使用 CGAffineTransformMakeRotation 方法..
this *** posting
确实,这解决了我的问题。
【讨论】:
以上是关于iPhone 纵向/横向和坐标系的主要内容,如果未能解决你的问题,请参考以下文章