Last active
November 12, 2019 11:34
-
-
Save DaisukeNagata/302d4966e01c68723dae09c151c9707e to your computer and use it in GitHub Desktop.
SampleKVO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// SampleObserve | |
// | |
// Created by 永田大祐 on 2019/11/12. | |
// Copyright © 2019 永田大祐. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
private var keyValueObservations = [NSKeyValueObservation]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let kvo = observe(\.self.view?.backgroundColor, options: .new) { sss, change in | |
guard let backgColor = change.newValue else { return } | |
print(backgColor ?? UIColor.white) | |
} | |
keyValueObservations.append(kvo) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(true) | |
self.view?.backgroundColor = UIColor(red: 0.123, green: 0.456, blue: 0.789, alpha: 1) | |
keyValueObservations.removeAll() | |
} | |
} | |
func convertToRGB(_ color: UIColor) -> (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { | |
let components = color.cgColor.components! | |
return (red: components[0], green: components[1], blue: components[2], alpha: components[3]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UIExtendedSRGBColorSpace 0.123 0.456 0.789 1