【发布时间】:2015-02-13 16:10:29
【问题描述】:
我有一个自定义的UIView,即使界面方向发生变化,我也想将它贴在UIWindow 的顶部。这是我的纵向视图
问题在于,先前的 iOS 8 UIWindow 坐标系不会随着方向的变化而改变。所以我需要手动进行所有计算。
首先是改变UIView的变换,我就是用这个方法做的
-(CGFloat)angleForOrientation:(UIInterfaceOrientation)orientation
{
CGFloat angle;
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
angle = -M_PI /2.0;
NSLog(@"UIInterfaceOrientationLandscapeLeft");
break;
case UIInterfaceOrientationLandscapeRight:
angle = M_PI /2.0;
NSLog(@"UIInterfaceOrientationLandscapeRight");
break;
case UIInterfaceOrientationPortraitUpsideDown:
angle = M_PI;
NSLog(@"UIInterfaceOrientationPortraitUpsideDown");
break;
default:
angle = 0;
NSLog(@"UIInterfaceOrientationPortrait");
break;
}
return angle;
}
第二件事是以某种方式将实际坐标系映射到 UIWindow 坐标系,它保持不变。
那么我应该如何计算自定义UIView 的框架,即使用户将视图旋转到其他方向,我也会将相同大小的 UIView 粘贴到屏幕的顶部中心?
例如,这就是视图在横向中的样子
顺便说一下,这张图片是从 iOS 8 版本生成的。为此我做了以下
self.frame = CGRectMake(window.bounds.size/2-50, 0, 100, 100);
CGFloat angle = [self angleForOrientation:orientation];
self.transform = CGAffineTransformMakeRotation(angle);
我需要做一些类似于 iOS 7 的事情。我怎样才能做到这一点?
感谢您的帮助!
【问题讨论】:
-
看看这个项目及其代码github.com/hfossli/AGWindowView
-
谢谢安德里亚。这是我尝试过的第一件事,但它并没有解决我的问题。可能我需要对源代码做一些修改。
标签: ios ios7 uiview ios8 uiwindow