[英]How to rotate and resize UIView on device rotation
我有一个UINavigationController应用程序,用户可以在几个导航视图上进行多个选择。 然后在最终视图上,我允许用户查看由他们从先前视图中的选择所定义的信息。 此信息显示在NavigationController堆栈的finalview的子视图中。
例:
UINavigationController
- Several Views including (final view)
Final View
- several Subviews
当最终视图加载后,我创建了一个执行didRotate方法的deviceOrientation侦听器。
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
然后,我调用一个称为currentView的方法,该方法用于确定当前可见的子视图。 即当我有类似[finalview.view addSubView:firstView.view]之类的东西时调用
[self copyCurrentView:@"firstView"];
- (void)copyCurrentView:(NSString *)currentView {
myCurrentView = currentView;
}
现在,当设备旋转时,将触发此方法
-(void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft)
{
if ([myCurrentView isEqualToString:@"firstView"]) {
[self.navigationController.view insertSubview:firstView.view aboveSubview:self.navigationController.navigationBar];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[firstView.view setBounds:CGRectMake(0, 0, 480.0, 320.0)];
[firstView.view setCenter:CGPointMake(320/2, 480/2)];
[firstView.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
}
//other if statments here
}
else if (orientation == UIDeviceOrientationLandscapeRight)
{
// do the same here
}
else if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
{
NSLog(@"@port");
if ([myCurrentView isEqualToString:@"firstView"]) {
[self.view insertSubview:firstView.view belowSubview:actionTabBar];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[firstView.view setBounds:CGRectMake(0.0, infoBarHeight, firstView.view.bounds.size.width, firstView.view.bounds.size.height)];
// [firstView.view setCenter:CGPointMake(480/2, 320/2)];
[firstView.view setTransform:CGAffineTransformMakeRotation(M_PI * 2)];
}
}
}
现在,这看起来有点杂乱无章,可能是一种非常随机的方式..但是,根据我在网上找到的所有内容,我无法弄清楚如何获得良好的效果。
目前,当我向左旋转设备时,它将完美地进入全屏显示。就像我想要的那样,当我再次旋转至纵向时,即使我使用正确的尺寸等,它也永远不会回到原始位置。
如果有人可以帮助我为当前可见的子视图设置旋转模型视图,将不胜感激。
我一直很犹豫地向我提出这个问题,很难以一种理解的方式来解释。.但是我尽了最大的努力来解释自己的所作所为以及我需要什么帮助。.希望有人可以帮我。
问候
这终于对我有用,它正在全屏旋转,希望对您有所帮助:
- (void)viewDidLoad
{
[super viewDidLoad];
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
// Do any additional setup after loading the view.
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
CGRect rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);;
switch (deviceOrientation) {
case 1:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
self.view.transform = CGAffineTransformMakeRotation(0);
self.view.bounds = [[UIScreen mainScreen] bounds];
[UIView commitAnimations];
break;
case 2:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitUpsideDown animated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
self.view.transform = CGAffineTransformMakeRotation(-M_PI);
self.view.bounds = [[UIScreen mainScreen] bounds];
[UIView commitAnimations];
break;
case 3:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
//rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
self.view.bounds = rect;
[UIView commitAnimations];
break;
case 4:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
//rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
self.view.bounds = rect;
[UIView commitAnimations];
break;
default:
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.