iOS UISwitch用法和示例

删除回忆录丶 提交于 2020-02-29 14:09:47

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!