ios - 如何计算与窗口大小相关的距离

标签 ios iphone swift uiviewcontroller

我想让 UIButton 适合所有 iPhone 屏幕:

  • iPhone 4s
  • iPhone 5
  • iPhone 5s
  • iPhone 6s Plus

如何计算与屏幕尺寸相关的距离?

我需要为所有 iPhone 屏幕安装暂停按钮和录制按钮。

实际上,它在 iPhone 6 上看起来很完美,但在 iPhone 6 plus 上看起来非常不同。

我没有使用 Storyboard!我只需要用代码来做这件事非常重要!

iPhone 6:

enter image description here

iPhone 6 Plus :

enter image description here

游戏 View Controller :

暂停按钮:

 func addPauseButton() {
        //create Pause Button
        PauseButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 + 150,self.view!.frame.size.height/2 - 315 ,30,30))

        PauseButton.setImage(UIImage(named: "Pause-Button.png"), forState: UIControlState.Normal)

        PauseButton.addTarget(self, action: "pressedPauseGame:", forControlEvents: .TouchUpInside)

        self.view.addSubview(PauseButton)
}

录制按钮:

func addRecordButton() {

            startRecordingButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 + 155 ,self.view!.frame.size.height/2 - 275 ,25,25))

            startRecordingButton.setImage(UIImage(named: "Record-Off.png"), forState: UIControlState.Normal)

            startRecordingButton.addTarget(self, action: "startRecordingButtonTapped:", forControlEvents: .TouchUpInside)

            self.view.addSubview(startRecordingButton)

            stopRecordingButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 + 155 ,self.view!.frame.size.height/2 - 275 ,25,25))

            stopRecordingButton.setImage(UIImage(named: "Record-On.png"), forState: UIControlState.Normal)

            stopRecordingButton.addTarget(self, action: "stopRecordingButtonTapped:", forControlEvents: .TouchUpInside)

            self.view.addSubview(stopRecordingButton)

    }

最佳答案

“StoryBoard”是您的 friend ,现在花点时间学习如何使用 StoryBoard 以在将来为自己节省大量时间,请参见下图,所有按钮在所有设备上都位于同一位置,零编码工作。有点绝妙的想法,一些编码中的老定时器没有使用,(或者编码器太习惯于对计算机屏幕上的每个 x,y 点进行编码)

不幸的是,一些老前辈进入了 Apple,然后我们失去了一些东西......比如 xCode 快照......真的太糟糕了,所以使用它或丢失它。

enter image description here

关于ios - 如何计算与窗口大小相关的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560309/

相关文章:

objective-c - 永远签署应用程序

ios - coccos2d游戏开发是否有用于设计物理的应用程序?

ios - 在未运行且 iBeacon 在范围内时调用应用程序

iphone - 如何检测 map 注释 Pin 上的点击?

ios - 如何在 iOS 9 中隐藏单个 View Controller 的状态栏?

iOS Appdelegate 多个第 3 方依赖项初始化会减慢应用程序启动速度

iphone - UIDatePicker 上下限

ios - Swift - 如何使用偏移选项将 UNIX 时间转换为工作日?

ios - 总是负速度 iOS

swift - 如何将 View Controller 推送到生成的导航层次结构中