class
UISwitch (swift))Xcode 7.0.1 + Swift 2.1html
3 步ios
E.X.
git
override func viewDidLoad() { super.viewDidLoad() var c = CGPointMake(100.0, 64.0) self.savedUISwt = self.createUISwitch(centerAt: c, dftOn: true, onSwtChangeSelector: "onSwtChange:") c = CGPointMake(100.0, 128.0) self.savedUISwt2 = self.createUISwitch(centerAt: c, dftOn: false, onSwtChangeSelector: "onSwtChange2") self.view.addSubview(self.savedUISwt!) self.view.addSubview(self.savedUISwt2!) }
E.X.
github
/* * NAME createUISwitch - limited func to createUISwitch * * NOTE * - only HERE (the VC use it -- selector) * - UISwitch is a class -- to return is Nice (other all func local) */ private func createUISwitch ( centerAt c: CGPoint, dftOn o: Bool, onSwtChangeSelector onSwtC: String) -> UISwitch { let swt = UISwitch() /* 設置位置 but size fixed !! */ swt.center = c; /* swt.frame = CGRectMake(c.x, c.y, w, h) */ /* 設置默認值 */ swt.on = o; /* on switch (on/off) change */ swt.addTarget(self, action: Selector(onSwtC), forControlEvents: UIControlEvents.ValueChanged) return swt }
有參數的更好點swift
E.X.
app
@IBAction private func onSwtChange (let sender: UISwitch) { if let s = self.savedUISwt { if (sender == s) { print(__LINE__, "my swt on: \(sender.on)") } } } @IBAction private func onSwtChange2 () { if let s = self.savedUISwt2 { print(__LINE__, "my swt2 on: \(s.on)") } }
Designated Initializeride
NOTE
: SIZE fixed !!ui
Swift
code
init(frame frame: CGRect)
Swift
htm
var on: Bool
Swift
func setOn(_ on: Bool, animated animated: Bool)
V
true if the switch should be turned to the On position
V
false if it should be turned to the Off position. If the switch is already in the designated position, nothing happens.
V
true to animate the 「flipping」 of the switch otherwise false.
onTintColor tintColor thumbTintColor onImage offImage
NSObject UIResponder UIView UIControl UISwitch
This table describes the changes to UISwitch Class Reference.
2013-09-18 Updated for iOS 7.
2012-09-19 Added new methods introduced in iOS 6.
2011-10-12 Updated for iOS 5.
2010-04-06 Fixed a typo.
2008-04-18 New document that describes the class for creating and managing the On/Off buttons known as switches