iOS UISwitch用法和示例
2015-10-05 00:00
148 查看
摘要: iOS UISwitch用法和示例.
示例使用Swift实现 - 动态创建 (代码中创建和设定UISwitch).
iOS UISwitch用法和示例 (
3 步
determining whether the UISwitch object is on or off.
optionally animating the transition.
animated
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
示例使用Swift实现 - 动态创建 (代码中创建和设定UISwitch).
iOS UISwitch用法和示例 (class
UISwitch (swift))
动态创建UISwitch (代码中创建) E.X.
Xcode 7.0.1 + Swift 2.13 步
1 在自己继承于UIViewController的VC中 例如在viewDidLoad
E.X.
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!) }
2 实现方法createUISwitch
E.X.
/* * 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 }
3 状态改变"回调" (Selector)实现
有参数的更好点E.X.
@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)") } }
init
Designated InitializerNOTE: SIZE fixed !!
Returns
an initialized switch object.Declaration
Swift
init(frame frame: CGRect)
Setting the Off/On State
on
A Boolean value that determines the off/on state of the switchDeclaration
Swift
var on: Bool
Discussion
This property allows you to retrieve and set (without animation) a valuedetermining whether the UISwitch object is on or off.
Availability
Available in iOS 2.0 and later.setOn
Set the state of the switch to On or Offoptionally animating the transition.
Declaration
Swift
func setOn(_ on: Bool, animated animated: Bool)
Parameters
onV
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.
animated
V
true to animate the “flipping” of the switch otherwise false.
Discussion
Setting the switch to either position does not result in an action message being sent.Availability
Available in iOS 2.0 and later.Customizing the Appearance of the Switch
onTintColor tintColor thumbTintColor onImage offImage
Inherits From
NSObject UIResponder UIView UIControl UISwitch
Document Revision History
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
Ref
UISwitch Class Reference(developer.apple.com)
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- Apple Swift学习教程
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift中定义二维数组的方法及遍历方法示例
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- 简单分析Swift语言的一些基本特征
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- IOS 改变键盘颜色代码