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

Dan*_* G. 5 objective-c sensor proximity 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
}
Run Code Online (Sandbox Code Playgroud)

编辑1:我试过的是这样的:

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

        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(proximityStateDidChange()), name:UIDeviceProximityStateDidChangeNotification, object: nil);
}
Run Code Online (Sandbox Code Playgroud)

和功能:

func proximityStateDidChange() {
        //DO WHATEVER I WANT
}
Run Code Online (Sandbox Code Playgroud)

我在执行应用程序时始终执行的功能.

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

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

override func viewDidLoad() {
        super.viewDidLoad()
        sensor.activateProximitySensor()
}
Run Code Online (Sandbox Code Playgroud)

抛出我的异常: 异常xcode抛弃我

希望有人能提供帮助,

提前致谢!

War*_*ing 7

Swift 3版

(根据Eric Aya的回答)

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!")
    }
}
Run Code Online (Sandbox Code Playgroud)


Dan*_* G. 6

最后,我得到了 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)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

并在viewDidLoad

override func viewDidLoad() {
  super.viewDidLoad()
  activateProximitySensor()
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


aya*_*aio 5

这是我对此的看法.

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!")
    }
}
Run Code Online (Sandbox Code Playgroud)


Iva*_*lod 5

斯威夫特 4.2

激活或停用接近传感器:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

选择器:

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