Transferred from: http://my.oschina.net/wolx/blog/396680
- A brief introduction to Uiswitch
- Two Uiswitch creation
- Three sets the selected state
- Quad Add monitor
- Five Test code
- 5.1 Code
- 5.2 Execution Results
A brief introduction to Uiswitch
The role of Uiswitch is to provide the user with a switch, which is very common in the Setup interface of the system, and the controls are simple.
Two Uiswitch creation
//Create Uiswitch *switch1 = [[Uiswitch alloc]init]; Cgsize viewsize = self.view.bounds. size;switch1.frame = CGRectMake (viewsize. height*0.2, 0, 0); //Use the initWithFrame method to initialize the switch control. CGRect rect = CGRectMake (viewsize. height*0.2, 0, 0); Uiswitch *SWITCH2 = [[Uiswitch alloc]initwithframe:rect];
Three sets the selected state
@property(nonatomic,getter=isOn) BOOL on;
The on property is used to control the state of the switch, and if set to Yes means open, if no it is closed and can be judged by the ISON side.
//1 设置开关状态//1.1 setOn 方法[switch1 setOn:YES]; //1.2 setOn:animated:方法。Animated 参数是布尔类型,若值为 YES 开关改变状态时会显 示动画[switch2 setOn:YES animated:YES]//2 判断状态if ([switch1 isOn]){ NSLog(@"The switch is on.");} else { NSLog(@"The switch is off.");}
Quad Add monitor
If you want to be notified when the switch control is turned on or off, you can use the Uiswitch addTarget:action:forControlEvents: Method plus the switch target.
1. Add Listener [Switch1Addtarget:selfAction@selector (switchischanged:) forcontrolevents:uicontroleventvaluechanged]; //2. The method that executes after the event occurs /** * Switchischanged method for listening for Uiswitch control's value change * * @param swith swith control */-(void)switch Ischanged: (Uiswitch *) swith{ if ([Swith isOn]) {NSLog (@"the switch is on."); } else {NSLog (@"The switch is off."); } }
Five Test Code 5.1 code
#import"ViewController.h"@interfaceViewcontroller ()@end@implementationviewcontroller-(void) Viewdidload {[Super Viewdidload];1. Create Uiswitch *switch1 = [[Uiswitch alloc]init];Cgsize viewsize =Self. View. Bounds. size; Switch1. frame = CGRectMake (viewsize. height*0.2,150,0,0);1.1 Initializes the switch control using the initWithFrame method.CGRect rect = CGRectMake (viewsize. height*0.2,250,0,0); Uiswitch *SWITCH2 = [[Uiswitch alloc]initwithframe:rect];2 Set Default check@property (Nonatomic,getter=ison) BOOL on; [Switch1 setOn:YES];2.1 seton:animated: Method. The animated parameter is a Boolean type, and if the value is YES, the animation is displayed when the switch changes state [SWITCH2 setOn:YES Animated:YES];3. Decide whether to selectif ([Switch1 isOn]) {NSLog (@ "The switch is on.");else {NSLog (@ "The switch is off.");4 If you want to be notified when a switch control is turned on or off, you must use Uiswitch's addTarget:action:forControlEvents: Method plus the target of the switch in your class. The following code: [Switch1 addTarget:Self action:@selector (switchischanged:) forcontrolevents:uicontroleventvaluechanged];Add to UIView [Self. View Addsubview:switch1]; [Self. View ADDSUBVIEW:SWITCH2]; }/** * switchischanged method for monitoring the value change of the Uiswitch control * * @param swith swith Control */-(void) Switchischanged: (Uiswitch *) swith{ if ([Swith isOn]) { NSLog (@ "The switch is on."); else { NSLog (@ "The switch is off.");}} -(void) didreceivememorywarning {[super didreceivememorywarning]; //Dispose of any resources, can be recreated.} @end
5.2 Execution Results
2015-04-0700:00:59.4352uiswitch[1220:29996] TheSwitchIsOn.2015-04-0700:01:06.1342uiswitch[1220:29996] TheSwitchIsOff.2015-04-0700:01:08.4242uiswitch[1220:29996] TheSwitchIsOn.2015-04-0700:11:57.6852uiswitch[1220:29996] TheSwitchIsOff.2015-14L0700:12:03.6812uiswitch[1220:29996] TheSwitchIsMnl2015-04-07 00:12:04.219 2uiswitch[1220:29996] the switch is off.2015-04-07 00 : 12:04.965 2UISwitch[1220:29996] the switch is on.
IOS Uiswitch Controls