-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUISwitch+Extensions.swift
63 lines (54 loc) · 1.49 KB
/
UISwitch+Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// UISwitch+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//
import UIKit
extension UISwitch: OYInit {
public typealias ViewType = UISwitch
/// Initializer method with `isOn` value
public static func oy_init(isOn: Bool) -> UISwitch {
let uiSwitch: UISwitch = oy_init()
uiSwitch.isOn = isOn
return uiSwitch
}
/// Get and Set `isOn` and fire `valueChanged` event
public var oy_isOn: Bool {
get { isOn }
set(value) {
isOn = value
sendActions(for: .valueChanged)
}
}
/// Get and Set thumbTintColor
public var oy_thumbColor: UIColor? {
get { thumbTintColor }
set(value) { thumbTintColor = value }
}
/// Get and Set ON state's tint color
public var oy_onColor: UIColor? {
get { onTintColor }
set(value) { onTintColor = value }
}
/// Get and Set OFF state's tint color
public var oy_offColor: UIColor? {
get { tintColor }
set(value) {
tintColor = value
backgroundColor = value
layer.cornerRadius = frame.height / 2.0
}
}
/// ON/OFF value toggle
public func oy_toggle(animated: Bool = true) {
setOn(!isOn, animated: animated)
sendActions(for: .valueChanged)
}
/// Observe `.valueChanged` event
public func oy_valueChange(closure: @escaping () -> Void) {
oy_event(for: .valueChanged) {
closure()
}
}
}