1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSの照度センサーを使って画面を暗くしたり明るくしたりする

Last updated at Posted at 2019-04-05

iOSの照度センサーを使って、なにかできないか模索していたときのメモ

iPhoneのカメラ部分を触ると画面を暗くできる

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 近接監視が有効(true)か無効かを示すブール値
        UIDevice.current.isProximityMonitoringEnabled = true
        //照度センサーを監視
        NotificationCenter.default.addObserver(self,
                                               selector: Selector("proximityChanged"),
                                               name:UIDevice.proximityStateDidChangeNotification,
                                               object: nil)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    @objc func proximityChanged() {
        //状態を表示
        print("\(UIDevice.current.proximityState)")
    }
}

1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?