获取横向视图的中心屏幕位置
目前我在窗口中有一个 UIImageView
。当场景加载时,我保存它的位置:
imageView.center
用户可以拖动它。在某些情况下,该项目会以动画方式返回到已保存的原始位置。当我垂直握住 iPad 时,一切正常。
当我水平握住它时,虽然我无法将其移回该位置,因为在横向模式下我无法使用在垂直方向模式下保存的位置。这是因为在 Interface Builder 中我将项目设置为相对于边框自动调整大小(这意味着如果该项目位于中心,然后旋转 iPad 它仍然保持在中心)
所以我的问题是:如何获得正确的原始值该项目在横向方向上的位置?
我尝试像这样手动计算位置:
- (CGPoint)getHorizontalCoordinatesForPoint:(CGPoint)point
{
CGSize size = [[UIScreen mainScreen]applicationFrame].size;
CGFloat relativeX = (point.x / (size.width));
CGFloat relativeY = point.y / (size.height);
CGFloat horizontalScreenX = relativeX * (size.height);
CGFloat horizontalScreenY = relativeY * (size.width);
return CGPointMake(horizontalScreenX, horizontalScreenY);
}
但是这个位置有点偏离。我认为这是因为我没有考虑导航栏的大小。 ios 中是否有一些转换功能已经可以满足我的要求?
At the moment I have an UIImageView
inside a window. When the scene loads I save its position with:
imageView.center
The user can drag it around. On some occasions the item gets animated back to its original position which has been saved. This all works fine, when I hold the IPad vertically.
When I hold it horizontally though I cant move it back to that position, because in landscape mode I cant use the position which was saved in vertical orientation mode. this is because in Interface Builder I set the item to get auto sized relative to the borders (meaning if the item was in the center, then rotating the IPad it still stays in center)
So my question is: How can I get the correct original position the item would have in landscape orientation?
I tried to calculate the position by hand like this:
- (CGPoint)getHorizontalCoordinatesForPoint:(CGPoint)point
{
CGSize size = [[UIScreen mainScreen]applicationFrame].size;
CGFloat relativeX = (point.x / (size.width));
CGFloat relativeY = point.y / (size.height);
CGFloat horizontalScreenX = relativeX * (size.height);
CGFloat horizontalScreenY = relativeY * (size.width);
return CGPointMake(horizontalScreenX, horizontalScreenY);
}
but this position is a bit off. I think it is because I dont take into account the size of the navigation bar. Is there some converting function in ios which already does what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。
相反,不要在场景加载时保存 [imageView center],而是在关键点保存它:
-touchesBegan:withEvent: (或者,如果您使用手势识别器,则在手势识别器回调中保存)。
-didRotateFromInterfaceOrientation:
次要的复杂情况是,如果用户在横向工作、退出应用程序、旋转纵向并重新启动应用程序,会发生什么情况。在这种情况下,我会以横向方式启动应用程序,然后让自动旋转纵向启动并纠正您的中心点。
Don't.
Rather, instead of saving [imageView center] when the scene loads, save it at key points:
-touchesBegan:withEvent: (or, if you are using gesture recognizers, in the gesture recognizer callback).
-didRotateFromInterfaceOrientation:
The minor complication is what happens if a user, say, is working in landscape, quits the app, rotates the portrait, and relaunches the app. In this case I would start the app in landscape and let the autorotation to portrait kick in and correct your center point.