我想在我的快速iPhone / iPad应用程序中进行分割视图或在iPhone上运行时会发生一些事情。基于这个答案(Detect if app is running in Slide Over or Split View mode in iOS 9),我已经想出了如何在Objective-C中做到这一点。我没有使用拆分视图控制器(如果拆分视图处于活动状态,则会对次要事情产生压力)。是否可以在实时时间内检查应用窗口的宽度?
基本上,当窗口宽度低于某个值时,我只想在我的应用中发生一些事情。
答案 0 :(得分:-1)
据我所知,我已经在swift中制作了第一个简单的分割视图检测器(感谢Ulysses R.)!
因此,根据Ulysses的建议,我使用let width = UIScreen.mainScreen().applicationFrame.size.width
来检测应用程序窗口的宽度。为了让计算机一遍又一遍地检查宽度,我每隔百分之一秒运行一次NSTimer,然后在宽度高于/低于某些东西的情况下进行操作。
为您量身定做的一些测量(您必须确定在上方/下方出现的宽度):
iPhone 6S Plus:414.0
iPhone 6S:375.0
iPhone 5S:320.0
iPad(肖像):768.0
iPad(1/3分割视图):320.0
iPad Air 2(1/2分割视图):507.0
iPad(风景):1024.0
以下是代码段:
class ViewController: UIViewController {
var widthtimer = NSTimer()
func checkwidth() {
var width = UIScreen.mainScreen().applicationFrame.size.width
if width < 507 { // The code inside this if statement will occur if the width is below 507.0mm (on portrait iPhones and in iPad 1/3 split view only). Use the measurements provided in the Stack Overflow answer above to determine at what width to have this occur.
// do the thing that happens in split view
textlabel.hidden = false
} else if width > 506 {
// undo the thing that happens in split view when return to full-screen
textlabel.hidden = true
}
}
override func viewDidAppear(animated: Bool) {
widthtimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "checkwidth", userInfo: nil, repeats: true)
// runs every hundredth of a second to call the checkwidth function, to check the width of the window.
}
override func viewDidDisappear(animated: Bool) {
widthtimer.invalidate()
}
}