Slide 1

Slide 1 text

iOSͰDarkModeΛ࣮૷͢Δ 2018.7.10 iOSLT / Shota Nakagami (@shtnkgm)

Slide 2

Slide 2 text

Mojaveͷ৽ػೳDarkMode iOSͷαϙʔτ͸ͳ͍͚Ͳ࣮૷ͯ͠ΈΔ࿩

Slide 3

Slide 3 text

DarkModeͬͯͲΜͳͷ?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

DarkMode͔͍͍ͬ͜

Slide 7

Slide 7 text

DarkMode͸໨ʹ΍͍͞͠

Slide 8

Slide 8 text

DarkMode

Slide 9

Slide 9 text

ࣗ෼Ͱ࣮૷͢Δ

Slide 10

Slide 10 text

഑৭Λߟ͑Δ • ௨ৗ࣌ͱDarkModeͷ࣌ͲΜͳ৭ʹ͢Δ͔ • Human Interface GuidelinesʹMacOSͰͷDarkModeͷํ਑͋Γ • ͨͩ୯७ʹ໌౓Λ൓స͢Ε͹ྑ͍Θ͚Ͱ͸ͳ͍ • ݟ΍͍͢Α͏ʹDarkModeͰͷ৭ͷௐ੔͕ඞཁ • ۩ମతͳ৭ͷࢦఆ஋͸Google Material Design͕ࢀߟʹͳΔ

Slide 11

Slide 11 text

഑৭Λ΋ͱʹUIColorΛ֦ு͢Δ

Slide 12

Slide 12 text

UI DarkMode ௨ৗ ϝΠϯςΩετ ϗϫΠτʢα=100%ʣ ϒϥοΫʢα=87%ʣ αϒςΩετ ϗϫΠτʢα=70%ʣ ϒϥοΫʢα=54%ʣ DisabledςΩετ ϗϫΠτʢα=50%ʣ ϒϥοΫʢα=38%ʣ ΞΫςΟϒΞΠίϯ ϗϫΠτʢα=100%ʣ ϒϥοΫʢα=54%ʣ ඇΞΫςΟϒΞΠίϯ ϗϫΠτʢα=38%ʣ ϒϥοΫʢα=50%ʣ σΟόΠμʔ άϨʔʢ#303030ʣ ϒϥοΫʢα=12%ʣ πʔϧόʔ άϨʔʢ#212121ʣ άϨʔʢ#F5F5F5ʣ Χʔυ άϨʔʢ#424242ʣ ϗϫΠτʢα=100%ʣ എܠ άϨʔʢ#303030ʣ άϨʔʢ#FAFAFAʣ

Slide 13

Slide 13 text

UIColorͷϓϥΠϕʔτExtensionͰෆಁ໌৭༻ͷม਺Λ࡞੒ private extension UIColor { static var gray050: UIColor { return color("#FAFAFA") } static var gray100: UIColor { return color("#F5F5F5") } static var gray200: UIColor { return color("#EEEEEE") } static var gray300: UIColor { return color("#E0E0E0") } static var gray400: UIColor { return color("#BDBDBD") } static var gray500: UIColor { return color("#9E9E9E") } static var gray600: UIColor { return color("#757575") } static var gray700: UIColor { return color("#616161") } static var gray800: UIColor { return color("#424242") } static var gray850: UIColor { return color("#303030") } static var gray900: UIColor { return color("#212121") } }

Slide 14

Slide 14 text

UIColorͷϓϥΠϕʔτExtensionͰಁ໌৭༻ͷม਺Λ࡞੒ private extension UIColor { static var blackHalfTranslucent: UIColor { return .init(white: 0, alpha: 0.5) } static var black012: UIColor { return .init(white: 1.00 - 0.12, alpha: 1) } static var black038: UIColor { return .init(white: 1.00 - 0.38, alpha: 1) } static var black050: UIColor { return .init(white: 1.00 - 0.50, alpha: 1) } static var black054: UIColor { return .init(white: 1.00 - 0.54, alpha: 1) } static var black070: UIColor { return .init(white: 1.00 - 0.70, alpha: 1) } static var black087: UIColor { return .init(white: 1.00 - 0.87, alpha: 1) } static var black100: UIColor { return .init(white: 1.00 - 1.00, alpha: 1) } static var whiteHalfTranslucent: UIColor { return .init(white: 1, alpha: 0.5) } static var white012: UIColor { return .init(white: 0.12, alpha: 1) } static var white038: UIColor { return .init(white: 0.38, alpha: 1) } static var white050: UIColor { return .init(white: 0.50, alpha: 1) } static var white054: UIColor { return .init(white: 0.54, alpha: 1) } static var white070: UIColor { return .init(white: 0.70, alpha: 1) } static var white087: UIColor { return .init(white: 0.87, alpha: 1) } static var white100: UIColor { return .init(white: 1.00, alpha: 1) } }

Slide 15

Slide 15 text

UIColorͷύϒϦοΫExtensionͰ෦඼৭ͷม਺Λ࡞੒ public extension UIColor { static var primary: UIColor { return Config.shared.primaryColor } static var activeIcon: UIColor { return isDarkTheme ? .white100 : .black054 } static var inactiveIcon: UIColor { return isDarkTheme ? .white038 : .black050 } static var primaryText: UIColor { return isDarkTheme ? .white100 : .black087 } static var secondaryText: UIColor { return isDarkTheme ? .white070 : .black054 } static var disabledText: UIColor { return isDarkTheme ? .white050 : .black038 } static var linkText: UIColor { return .flatSkyBlue() } static var dividers: UIColor { return isDarkTheme ? .gray850 : .black012 } static var statusBar: UIColor { return isDarkTheme ? .black100 : .gray300 } static var appBar: UIColor { return isDarkTheme ? .gray900 : .gray100 } static var highlightedAppBar: UIColor { return isDarkTheme ? .gray800 : .gray200 } static var background: UIColor { return isDarkTheme ? .gray850 : .gray050 } static var card: UIColor { return isDarkTheme ? .gray800 : .white100 } static var highlightedCard: UIColor { return isDarkTheme ? .gray850 : .gray100 } }

Slide 16

Slide 16 text

৭Λࢦఆ͢Δ backgroundColor = .card mainLabel.textColor = .primaryText settingSwitch.onTintColor = .primary

Slide 17

Slide 17 text

ΞΠίϯը૾͸Ͳ͏͢Δʁ

Slide 18

Slide 18 text

UIImageΛUIColorͰృΕΔΑ͏ʹ͢Δ

Slide 19

Slide 19 text

public extension UIImage { public func image(withTint color: UIColor) -> UIImage { let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) UIGraphicsBeginImageContextWithOptions(rect.size, false, 0) guard let context: CGContext = UIGraphicsGetCurrentContext(), let cgImage = cgImage else { return UIImage() } context.scaleBy(x: 1, y: -1) context.translateBy(x: 0, y: -self.size.height) context.clip(to: rect, mask: cgImage) context.setFillColor(color.cgColor) context.fill(rect) guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return UIImage() } UIGraphicsEndImageContext() return image } }

Slide 20

Slide 20 text

let iconImage = UIImage(named: "icon").image(withTint: .activeIcon)

Slide 21

Slide 21 text

image(withTint: )͸OSSͷta1n/Swi3ExtensionsͰར༻Մ

Slide 22

Slide 22 text

ࣗ෼΋contribu)onͯ͠·͢

Slide 23

Slide 23 text

ΊͰͨ͠ΊͰͨ͠

Slide 24

Slide 24 text

ΊͰͨ͠ΊͰͨ͠

Slide 25

Slide 25 text

Ϟʔυ੾Γସ͑࣌ʹ৭ͷมߋ͕ඞཁ DarkMode⁶௨ৗ

Slide 26

Slide 26 text

͜Ε͕΍Γ͍ͨ ΈΜͳʙ৭Λߋ৽ͯ͠ʙ

Slide 27

Slide 27 text

௨஌ઌ͕nݸͷ௨஌ͷ࢓૊Έͱ͍͑͹ʁ

Slide 28

Slide 28 text

No#fica#onCenter ࢀߟ: shtnkgm / Swi.ͱΦϒδΣΫτؒͷ௨஌ͷύλʔϯ

Slide 29

Slide 29 text

࣮૷֓ཁ 1. ֤ViewͰ௨஌Λߪಡ͓ͯ͘͠ 2. Ϟʔυͷมߋ࣌ʹNo*fica*onCenterͰ௨஌ 3. ֤ViewͰ௨஌Λड͚औΔ 4. ֤ViewͰ৭Λߋ৽

Slide 30

Slide 30 text

֤ViewͰड৴ॲཧΛॻ͘ͷ͸໘౗ public extension Notification.Name { static let styleUpdated = Notification.Name("styleUpdated") } class HogeView { init() { let center = NotificationCenter.default center.addObserver(self, selector: #selector(updateStyle), name: .styleUpdated, object: nil) updateStyle() } @objc func updateStyle() { backgroundColor = .card } }

Slide 31

Slide 31 text

ϓϩτίϧͰॲཧΛڞ௨Խ

Slide 32

Slide 32 text

StyleUpdatable

Slide 33

Slide 33 text

public protocol StyleUpdatable: class { /// ௨஌Λղআ͢Δ৔߹͸ObserverΛอ࣋͢Δඞཁ͋Γ var updateStyleObserver: NSObjectProtocol? { get set } /// ৭ͷߋ৽ॲཧ͸ඞͣupdateStyleͱ͍͏໊લͷϝιουͰߦ͏ func updateStyle() }

Slide 34

Slide 34 text

public extension StyleUpdatable { /// updateStyleObserverΛ೚ҙ࣮૷ͱ͢ΔͨΊͷσϑΥϧτ࣮૷ var updateStyleObserver: NSObjectProtocol? { get { return nil } set { } } /// ߪಡ & updateStyle()Λ࣮ߦ func observeAndUpdateStyle() { updateStyle() let center = NotificationCenter.default updateStyleObserver = center.addObserver(forName: .styleUpdated, object: nil, queue: nil) { [weak self] _ in self?.updateStyle() } } }

Slide 35

Slide 35 text

class HogeView: StyleUpdatable { init() { observeAndUpdateStyle() } // StyleUpdatableʹΑΓ࣮૷͕ڧ੍͞ΕΔ func updateStyle() { backgroundColor = .card } }

Slide 36

Slide 36 text

ద༻αϯϓϧ ͡ͿΜͷΞϓϦͰಋೖ

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

ͦΕͬΆ͘ͳͬͨ

Slide 42

Slide 42 text

·ͱΊ • DarkModeͱ௨ৗ࣌ͷ഑৭Λߟ͑Δ • UIColorΛ֦ுͯ͠ɺDarkMode͔Ͳ͏͔Ͱ෼ذ (֤View͸DarkMode͔Ͳ͏͔Λߟྀ͠ͳͯ͘ྑ͍) • ΞΠίϯ͸UIImageΛUIColorͰృΕΔΑ͏ʹ͢Δ • StyleUpdatableϓϩτίϧͰ৭ͷߋ৽ॲཧΛڞ௨Խ

Slide 43

Slide 43 text

͓ΘΓ ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠