Created
March 14, 2017 06:17
-
-
Save npu3pak/824ac1c64326486f37bd7dd9a4fae33d to your computer and use it in GitHub Desktop.
Замена UISwitch с кастомными картинками для состояний
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
import UIKit | |
@IBDesignable class CustomSwitch: UIButton { | |
var onValueChanged: ((Bool) -> Void)? | |
@IBInspectable var isOn: Bool = false { | |
didSet { | |
isSelected = isOn | |
onValueChanged?(isOn) | |
} | |
} | |
@IBInspectable var onImage: UIImage? { | |
didSet { | |
setUp() | |
} | |
} | |
@IBInspectable var offImage: UIImage? { | |
didSet { | |
setUp() | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setUp() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
setUp() | |
} | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
setUp() | |
} | |
private func setUp() { | |
clipsToBounds = true | |
isSelected = false | |
setImage(onImage, for: .selected) | |
setImage(offImage, for: .normal) | |
addTarget(self, action: #selector(invertIsOn), for: .touchDown) | |
} | |
func invertIsOn() { | |
isOn = !isOn | |
} | |
} |
Author
npu3pak
commented
Mar 14, 2017
- Делаем кнопку типа Custom.
- Назначаем кнопке тип CustomSwitch.
- Задаем картинки для состояний
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment