Ios 近距离监视可能无法按预期工作
对于我的iOS应用程序,我想实现一个功能,当设备面朝下时,屏幕应该关闭(比如当你接听电话时)。 所以我从检测设备方向开始:Ios 近距离监视可能无法按预期工作,ios,swift,swift2,Ios,Swift,Swift2,对于我的iOS应用程序,我想实现一个功能,当设备面朝下时,屏幕应该关闭(比如当你接听电话时)。 所以我从检测设备方向开始: //in my ViewDidLoad NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.rotated(_:)), name: UIDeviceOrientationDidChangeNotification, object: nil) //called
//in my ViewDidLoad
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.rotated(_:)), name: UIDeviceOrientationDidChangeNotification, object: nil)
//called when the device changes orientation
func rotated(notification: NSNotification){
if UIDevice.currentDevice().orientation == UIDeviceOrientation.FaceDown{
print("device = faced down")
}else{
print("device != faced down")
}
}
当设备停机时,我会打电话给你
UIDevice.currentDevice().proximityMonitoringEnabled = true
否则
问题是UIDeviceOrientationIDChangeNotification
似乎动作有点晚,因此当调用rotated()
函数时,设备已经面朝下,结果表明,为了使proximityMonitoringEnabled=true
关闭屏幕,不应该已经覆盖接近传感器!
我很确定这是苹果公司的局限性,但也许有人找到了解决方案或找到了解决办法!
提前感谢。方法:
class ProximityViewController: UIViewController {
let cmManager = CMMotionManager(), gyroData = (minX:-3.78, minY:-3.38, minZ:-5.33, maxX:3.29, maxY:4.94, maxZ:3.36)
override func viewDidLoad() {
super.viewDidLoad()
//Using GyroMotion
experimentCoreMotion()
}
//MARK: - Using Core Motion
func experimentCoreMotion() {
if cmManager.gyroAvailable {
//Enable device orientation notification
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
cmManager.gyroUpdateInterval = 0.1
handleFaceDownOrientation()
}
}
func handleFaceDownOrientation() {
cmManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: { (data:CMGyroData?, error: NSError?) in
if self.isGyroDataInRange(data!) {
UIDevice.currentDevice().proximityMonitoringEnabled = (UIDevice.currentDevice().orientation == .FaceDown)
if UIDevice.currentDevice().orientation == .FaceDown { print("FaceDown detected") }
else { print("Orientation is not facedown") }
}
})
}
func isGyroDataInRange(val: CMGyroData) -> Bool {
return ((val.rotationRate.x > gyroData.minX && val.rotationRate.x < gyroData.maxX) &&
(val.rotationRate.y > gyroData.minY && val.rotationRate.y < gyroData.maxY) &&
(val.rotationRate.z > gyroData.minZ && val.rotationRate.z < gyroData.maxZ))
}
}
由于iOS在更改方向之前不提供,我们不能依赖“UIDeviceOrientationDidChangeNotification”。相反,我们可以使用CoreMotion框架和访问硬件的陀螺仪来检测可能的面朝下方向,并适当地设置proximityMonitoringEnabled
陀螺仪数据:
class ProximityViewController: UIViewController {
let cmManager = CMMotionManager(), gyroData = (minX:-3.78, minY:-3.38, minZ:-5.33, maxX:3.29, maxY:4.94, maxZ:3.36)
override func viewDidLoad() {
super.viewDidLoad()
//Using GyroMotion
experimentCoreMotion()
}
//MARK: - Using Core Motion
func experimentCoreMotion() {
if cmManager.gyroAvailable {
//Enable device orientation notification
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
cmManager.gyroUpdateInterval = 0.1
handleFaceDownOrientation()
}
}
func handleFaceDownOrientation() {
cmManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: { (data:CMGyroData?, error: NSError?) in
if self.isGyroDataInRange(data!) {
UIDevice.currentDevice().proximityMonitoringEnabled = (UIDevice.currentDevice().orientation == .FaceDown)
if UIDevice.currentDevice().orientation == .FaceDown { print("FaceDown detected") }
else { print("Orientation is not facedown") }
}
})
}
func isGyroDataInRange(val: CMGyroData) -> Bool {
return ((val.rotationRate.x > gyroData.minX && val.rotationRate.x < gyroData.maxX) &&
(val.rotationRate.y > gyroData.minY && val.rotationRate.y < gyroData.maxY) &&
(val.rotationRate.z > gyroData.minZ && val.rotationRate.z < gyroData.maxZ))
}
}
使用陀螺仪观测值低于此值可能会检测到面朝下的方向
let gyroData = (minX:-3.78, minY:-3.38, minZ:-5.33, maxX:3.29, maxY:4.94, maxZ:3.36)
Swift中的解决方案:
class ProximityViewController: UIViewController {
let cmManager = CMMotionManager(), gyroData = (minX:-3.78, minY:-3.38, minZ:-5.33, maxX:3.29, maxY:4.94, maxZ:3.36)
override func viewDidLoad() {
super.viewDidLoad()
//Using GyroMotion
experimentCoreMotion()
}
//MARK: - Using Core Motion
func experimentCoreMotion() {
if cmManager.gyroAvailable {
//Enable device orientation notification
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
cmManager.gyroUpdateInterval = 0.1
handleFaceDownOrientation()
}
}
func handleFaceDownOrientation() {
cmManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: { (data:CMGyroData?, error: NSError?) in
if self.isGyroDataInRange(data!) {
UIDevice.currentDevice().proximityMonitoringEnabled = (UIDevice.currentDevice().orientation == .FaceDown)
if UIDevice.currentDevice().orientation == .FaceDown { print("FaceDown detected") }
else { print("Orientation is not facedown") }
}
})
}
func isGyroDataInRange(val: CMGyroData) -> Bool {
return ((val.rotationRate.x > gyroData.minX && val.rotationRate.x < gyroData.maxX) &&
(val.rotationRate.y > gyroData.minY && val.rotationRate.y < gyroData.maxY) &&
(val.rotationRate.z > gyroData.minZ && val.rotationRate.z < gyroData.maxZ))
}
}
类ProximityViewController:UIViewController{
设cmManager=CMMotionManager(),gyroData=(最小值:-3.78,最小值:-3.38,最小值:-5.33,最大值:3.29,最大值:4.94,最大值:3.36)
重写func viewDidLoad(){
super.viewDidLoad()
//使用回转运动
运动
}
//马克:-使用核心运动
func运动(){
如果cmManager.0可用{
//启用设备定向通知
UIDevice.currentDevice().BegingeratingDeviceOrientationNotifications()
cmManager.gyroUpdateInterval=0.1
handleFaceDownOrientation()
}
}
func handleFaceDownOrientation(){
cmManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()),其中的处理程序:{(数据:CMGyroData?,错误:NSError?)
如果self.isGyroDataInRange(数据!){
UIDevice.currentDevice().proximityMonitoringEnabled=(UIDevice.currentDevice().orientation==.FaceDown)
如果UIDevice.currentDevice().orientation==.FaceDown{print(“检测到FaceDown”)}
else{打印(“方向不是面朝下”)}
}
})
}
func isGyroDataInRange(val:CMGyroData)->Bool{
返回((val.rotationRate.x>gyroData.minX&&val.rotationRate.xgyroData.minY和&val.rotationRate.ygyroData.minZ&&val.rotationRate.z
希望我的解决方案能解决你的疑问。让我知道该解决方案符合您的要求
陀螺仪和加速计观测:
class ProximityViewController: UIViewController {
let cmManager = CMMotionManager(), gyroData = (minX:-3.78, minY:-3.38, minZ:-5.33, maxX:3.29, maxY:4.94, maxZ:3.36)
override func viewDidLoad() {
super.viewDidLoad()
//Using GyroMotion
experimentCoreMotion()
}
//MARK: - Using Core Motion
func experimentCoreMotion() {
if cmManager.gyroAvailable {
//Enable device orientation notification
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
cmManager.gyroUpdateInterval = 0.1
handleFaceDownOrientation()
}
}
func handleFaceDownOrientation() {
cmManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: { (data:CMGyroData?, error: NSError?) in
if self.isGyroDataInRange(data!) {
UIDevice.currentDevice().proximityMonitoringEnabled = (UIDevice.currentDevice().orientation == .FaceDown)
if UIDevice.currentDevice().orientation == .FaceDown { print("FaceDown detected") }
else { print("Orientation is not facedown") }
}
})
}
func isGyroDataInRange(val: CMGyroData) -> Bool {
return ((val.rotationRate.x > gyroData.minX && val.rotationRate.x < gyroData.maxX) &&
(val.rotationRate.y > gyroData.minY && val.rotationRate.y < gyroData.maxY) &&
(val.rotationRate.z > gyroData.minZ && val.rotationRate.z < gyroData.maxZ))
}
}
我用陀螺仪和加速计试验了可能的面朝下方向值。在我看来,陀螺仪数据似乎很好,但可以探索其他硬件的传感器来检测面朝下的方向。虽然您的代码成功检测到了面朝下的状态,但我的问题仍然存在“为了使proximityMonitoringEnabled=true关闭屏幕,不应该已经覆盖接近传感器”我认为你的解决方案是出路,但我必须发挥更多的价值,以种检测(设备将旋转到面朝下)!我将在试用您的代码时等待其他解决方案!只要你能成功,我就把它作为一个有效的答案!嗯,好的,我会检查并更新我的答案。
let gyroData = (minX:-3.78, minY:-3.38, minZ:-5.33, maxX:3.29, maxY:4.94, maxZ:3.36)