您的位置:首页 > 产品设计 > UI/UE

iOS UISwitch用法和示例

2015-10-05 00:00 148 查看
摘要: iOS UISwitch用法和示例.
示例使用Swift实现 - 动态创建 (代码中创建和设定UISwitch).

iOS UISwitch用法和示例 (
class
UISwitch (swift))

动态创建UISwitch (代码中创建) E.X.

Xcode 7.0.1 + Swift 2.1

3 步

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 Initializer

NOTE
: 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 switch

Declaration

Swift


var on: Bool

Discussion

This property allows you to retrieve and set (without animation) a value
determining 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 Off
optionally animating the transition.

Declaration

Swift


func setOn(_ on: Bool,
animated animated: Bool)

Parameters

on

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.


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)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息