[英]Rotating an image 90 degrees on same position when orientation changes
结束这样做: 支持多个方向的最简单方法? 当应用程序在Landscape中时如何加载自定义NIB? 工作精湛!
我在Photoshop中制作了一个图像,我想在iPad应用程序中将其用作信息屏幕的背景。 该图像还包含文本和一些图标。 在图像周围,我有一个绿色的边框。
我想要达到的效果是:
当用户从纵向朝向横向时,我希望图像(仅框架和图标)旋转90度,使图像以横向模式显示,而不是在横向上具有框架的纵向视图。 文本和图标是分离的(我在不同的UIImageView中组织的不同层)它们将旋转90度。
我已经完成的工作如下:
用这种方法进行了一些实验:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:
并试图这样做:
self.btnFacebook.transform = CGAffineTransformMakeRotation(1.5707964);
我认为它会将btnFacebook属性向右旋转90度(如果我错了,请纠正我)因为它指定了正弧度。 我似乎无法让它正常工作。 难道这不能将按钮绕框架中心坐标旋转90度吗? 这不会导致按钮位置发生变化(按钮是方形)?
制作图像:
由于图像显示图像背景(其上的一些自定义图形在两个方向上看起来都很好)从纵向到横向并旋转,因此它在横向上不显示为肖像,图标与背景分离,因此它们需要旋转为因为他们需要处于正确的方向(这是社交图标)。 然而,文本位于相同的位置,它只旋转90度而不重新定位。
我理解你的问题是你试图不旋转整个界面,而是单独旋转紫色和红色方块。
我创建了一个类似于你的布局的UIViewController
。
黑色正方形是一个UIView,白色正方形只有这样我才能知道黑色视图何时旋转。 此视图连接到控制器上的view1
属性。
有4个按钮连接到btnx
(x运行1到4)属性。
由于我不再想要自动旋转界面,我只支持纵向方向。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
为了手动完成旋转,我向ViewController添加了一个方法。 它确定将组件从纵向旋转到当前方向所需的角度,创建旋转变换并将其应用于所有出口。
- (void)deviceDidRotate:(NSNotification *)notification
{
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
double rotation = 0;
UIInterfaceOrientation statusBarOrientation;
switch (currentOrientation) {
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationUnknown:
return;
case UIDeviceOrientationPortrait:
rotation = 0;
statusBarOrientation = UIInterfaceOrientationPortrait;
break;
case UIDeviceOrientationPortraitUpsideDown:
rotation = -M_PI;
statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
break;
case UIDeviceOrientationLandscapeLeft:
rotation = M_PI_2;
statusBarOrientation = UIInterfaceOrientationLandscapeRight;
break;
case UIDeviceOrientationLandscapeRight:
rotation = -M_PI_2;
statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
break;
}
CGAffineTransform transform = CGAffineTransformMakeRotation(rotation);
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.btn1 setTransform:transform];
[self.btn2 setTransform:transform];
[self.btn3 setTransform:transform];
[self.btn4 setTransform:transform];
[self.view1 setTransform:transform];
[[UIApplication sharedApplication] setStatusBarOrientation:statusBarOrientation];
} completion:nil];
}
最后要做的是让操作系统调用我的方法。 为此,我在application:didFinishLaunchingWithOptions:
添加了以下代码application:didFinishLaunchingWithOptions:
在AppDelegate中。
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self.viewController selector:@selector(deviceDidRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
我不确定这是否正是你想要的,但我相信它至少是相似的,所以你可以从中得到一些想法如何解决你的问题。 我可以为我创建的工作iPad应用程序提供源代码来说明这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.