Swift 中的接近传感器(来自 Objective-C)

Proximity sensor in Swift (from Objective-C)

我是 swift 的新用户,现在,我需要利用 iPhone 的接近传感器。我不在乎距离,但我想知道什么时候靠近 iPhone。

所以我在 Objective-C 中找到了这段有效的代码,但我在 Swift 中需要它。我尝试了一些方法,但都奏效了。所以这是我需要的代码:

- (void) activateProximitySensor {
    UIDevice *device = [UIDevice currentDevice];
    device.proximityMonitoringEnabled = YES;
    if (device.proximityMonitoringEnabled == YES) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityChanged:) name:@"UIDeviceProximityStateDidChangeNotification" object:device];
    }
}

- (void) proximityChanged:(NSNotification *)notification {
    UIDevice *device = [notification object];
    NSLog(@"Detectat");

    //DO WHATEVER I WANT
}

编辑 1: 我试过的是:

override func viewDidLoad() {
        super.viewDidLoad()
        UIDevice.currentDevice().proximityMonitoringEnabled = true;

        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(proximityStateDidChange()), name:UIDeviceProximityStateDidChangeNotification, object: nil);
}

和函数:

func proximityStateDidChange() {
        //DO WHATEVER I WANT
}

我放在函数中的内容总是在应用程序执行时执行。

编辑 2: 尝试 Eric D. 评论的代码

let sensor = MySensor() //declared in the VC but globally

override func viewDidLoad() {
        super.viewDidLoad()
        sensor.activateProximitySensor()
}

抛出异常:

希望有人能帮忙,

提前致谢!

这是我的看法。

func activateProximitySensor() {
    let device = UIDevice.currentDevice()
    device.proximityMonitoringEnabled = true
    if device.proximityMonitoringEnabled {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "proximityChanged:", name: "UIDeviceProximityStateDidChangeNotification", object: device)
    }
}

func proximityChanged(notification: NSNotification) {
    if let device = notification.object as? UIDevice {
        println("\(device) detected!")
    }
}

最后我得到了 Eric D 的回答。

代码如下:

func proximityChanged(notification: NSNotification) {
  if let device = notification.object as? UIDevice {
    println("\(device) detected!")
  }
}
        
func activateProximitySensor() {
  let device = UIDevice.currentDevice()
  device.proximityMonitoringEnabled = true
  if device.proximityMonitoringEnabled {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "proximityChanged:", name: "UIDeviceProximityStateDidChangeNotification", object: device)
    }
  }
}

并在 viewDidLoad 中:

override func viewDidLoad() {
  super.viewDidLoad()
  activateProximitySensor()
}

希望对您有所帮助!

Swift 3 版本

(基于

func setProximitySensorEnabled(_ enabled: Bool) {
    let device = UIDevice.current
    device.isProximityMonitoringEnabled = enabled
    if device.isProximityMonitoringEnabled {
        NotificationCenter.default.addObserver(self, selector: #selector(proximityChanged), name: .UIDeviceProximityStateDidChange, object: device)
    } else {
        NotificationCenter.default.removeObserver(self, name: .UIDeviceProximityStateDidChange, object: nil)
    }
}

func proximityChanged(_ notification: Notification) {
    if let device = notification.object as? UIDevice {
        print("\(device) detected!")
    }
}

Swift 4

//MARK:- Sensor deduct when hide and show the screen when call
func activateProximitySensor() {
    let device = UIDevice.current
    device.isProximityMonitoringEnabled = true
    if device.isProximityMonitoringEnabled {
        NotificationCenter.default.addObserver(self, selector: #selector(proximityChanged(notification:)), name: NSNotification.Name(rawValue: "UIDeviceProximityStateDidChangeNotification"), object: device)
    }
}

@objc func proximityChanged(notification: NSNotification) {
    if let device = notification.object as? UIDevice {
        print("\(device) detected!")
    }
}

用于移除观察者

 NotificationCenter.default.removeObserver(self, name: .UIDeviceProximityStateDidChange, object: nil)

Swift 4.2

激活或停用 ProximitySensor:

func activateProximitySensor(isOn: Bool) {
    let device = UIDevice.current
    device.isProximityMonitoringEnabled = isOn
    if isOn {
        NotificationCenter.default.addObserver(self, selector: #selector(proximityStateDidChange), name: UIDevice.proximityStateDidChangeNotification, object: device)
    } else {
        NotificationCenter.default.removeObserver(self, name: UIDevice.proximityStateDidChangeNotification, object: device)
    }
}

选择器:

@objc func proximityStateDidChange(notification: NSNotification) {
    if let device = notification.object as? UIDevice {
        print(device)
    }
}