func shouldirotate(){
var whichwaywhere: String {
if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft {
return "left"
}
if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight {
return "right"
}
if UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown {
return "down"
}
return "I don't Care"
}
println(whichwaywhere)
}
当我创建一个连续检查自身的函数时,(将shouldirotate附加到NSTimer)我可以检查方向。当UIDeviceOrientationDidChangeNotification激活时,我怎么能告诉函数运行?
要让UIDeviceOrientationDidChangeNotification运行,我有一个变量:
var rotatenote: Bool = UIDevice.currentDevice().generatesDeviceOrientationNotifications
并且在ViewDidLoad覆盖中我有:
override func viewDidLoad() {
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
rotatenote = true
}
这是使用和声明此属性的正确方法吗?如何让函数运行DeviceOrientationNotification?
P.S。:我在ViewDidLoad中有一个rotatenote = true,因为如果我试着像往常一样将它附加到变量声明中,它会说"不能分配给这个表达式的结果。"见下文
var rotatenote: Bool = UIDevice.currentDevice().generatesDeviceOrientationNotifications = true
答案 0 :(得分:3)
有几种方法可以做到这一点。不确定你担心什么操作系统,但在iOS 8中你有两个简单的选择。您可以注册
的观察者 UIDeviceOrientationDidChangeNotification
或者您可以覆盖
func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
你应该不需要
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
所以要么在viewDidLoad中添加:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "functionThatYouWantTriggeredOnRotation", name:
UIDeviceOrientationDidChangeNotification, object: nil)
你有一个函数functionThatYouWantTriggeredOnRotation
进行计算......
或者你可以只为viewWillTransition提供覆盖,而不必为UIDeviceOrientationDidChangeNotification
override func viewWillTransitionToSize(size: CGSize,
withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
if UIDevice.currentDevice().orientation.isLandscape {
//do your thing
}
}