使用 Objective C 在 iOS8 中分离横向和纵向视图
Posted
技术标签:
【中文标题】使用 Objective C 在 iOS8 中分离横向和纵向视图【英文标题】:Separate Landscape and Portrait Views in iOS8 using Objective C 【发布时间】:2015-01-28 20:10:27 【问题描述】:我在 ios7 中使用下面的代码有单独的横向和纵向视图:
-(void)didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1)
CGRect currentBounds=self.view.bounds;
if (iPadInt==0)
if (self.interfaceOrientation==UIInterfaceOrientationLandscapeRight)
self.view=self.landscapeQuestionView;
self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(90));
else if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft)
self.view=self.landscapeQuestionView;
self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(-90));
else if (self.interfaceOrientation==UIInterfaceOrientationPortrait)
self.view=self.portraitQuestionView;
self.view.transform=CGAffineTransformMakeRotation(0);
else if (self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
self.view=self.portraitQuestionView;
self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(180));
self.view.bounds=currentBounds;
else if (iPadInt==1)
if (self.interfaceOrientation==UIInterfaceOrientationLandscapeRight)
self.view=self.iPadLandscapeQuestionView;
self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(90));
rotationInt=90;
else if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft)
self.view=self.iPadLandscapeQuestionView;
self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(-90));
rotationInt=90;
else if (self.interfaceOrientation==UIInterfaceOrientationPortrait)
self.view=self.iPadPortraitQuestionView;
self.view.transform=CGAffineTransformMakeRotation(0);
rotationInt=0;
else if (self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
self.view=self.iPadPortraitQuestionView;
self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(180));
rotationInt=0;
self.view.bounds=currentBounds;
我知道我应该在 iOS8 中使用 viewWillTransitionToSize 方法,但我不知道如何在该方法中获取这些不同的视图。提前感谢您的帮助。
【问题讨论】:
【参考方案1】:这里有个话题:Rotation methods deprecated, equivalent of 'didRotateFromInterfaceOrientation'?
我认为在您的情况下,您可以像这样使用 viewWillTransitionToSize::
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
//get orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
//check orientation
if (orientation==UIInterfaceOrientationPortrait)
NSLog(@"portrait");
else if(orientation==UIInterfaceOrientationPortraitUpsideDown)
NSLog(@"upside-down");
else if(orientation==UIInterfaceOrientationLandscapeRight)
NSLog(@"landscape right");
else if(orientation==UIInterfaceOrientationLandscapeLeft)
NSLog(@"landscape left");
completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
];
【讨论】:
以上是关于使用 Objective C 在 iOS8 中分离横向和纵向视图的主要内容,如果未能解决你的问题,请参考以下文章