简体   繁体   English

我可以根据设备的方向更改绘制位置吗

[英]Can I change position of draw based on orientation of device

I am trying to create a very basic iOS app using swift 2.0.I have a background image that I would like to draw on top of, but I want the drawing to look consistent in landscape and portrait mode. 我正在尝试使用swift 2.0创建一个非常基本的iOS应用程序。我想在上面绘制背景图像,但是我希望该图在横向和纵向模式下看起来一致。 Using auto layout and constraints would be impossible because the image and its height to width ratio resize whenever the orientation is changed. 使用自动布局和约束将是不可能的,因为只要更改方向,图像及其高宽比就会调整大小。 I'm wondering if I could set some sort of offset from the edge of the page in the vertical and horizontal directions based on the orientation of the device. 我想知道是否可以根据设备的方向在垂直和水平方向上设置页面边缘的某种偏移量。 I have been trying to code it up but have been having trouble. 我一直在尝试编写代码,但是遇到了麻烦。 I looked into using UIDeviceOrientation but am not sure how exactly to implement it in code. 我研究了使用UIDeviceOrientation,但不确定如何在代码中准确实现它。 Any help would be appreciated 任何帮助,将不胜感激

I see two ways to do this off the top of my head.. I haven't tried coding it yet however. 我想到了两种方法来实现此目的。.但是我还没有尝试对其进行编码。

  1. set an outlet to the two constraints that's set's the x and y of what you draw on top of the background. 为两个约束设置出口,这两个约束是您在背景顶部绘制的对象的x和y。 Then on the device orientation change you can just change the constraint.constant to a new value that fits better. 然后在设备方向更改上,您只需将constraint.constant更改为更合适的新值即可。

  2. You can use the interface builders built in functionality to set constraint based on which sized screen you are dealing with.. You find it at the bottom of the interface builder main view... default values are "w: Any, h: Any" 您可以使用内置的界面构建器功能来根据要处理的屏幕大小设置约束。在界面构建器主视图的底部找到它。默认值为“ w:任意,h:任意”

Animal's second idea is great. 动物的第二个主意很棒。 You can try it here 你可以在这里尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM