The first way to create a Uiswitch control is to create it dynamically in your code.
1, open Xcode 4.3.2, new project switch, select Single View application.
2, open the viewcontroller.m file in the Viewdidload method to add code:
Copy Code code as follows:
-(void) viewdidload
{
[Super Viewdidload];
Uiswitch *switchbutton = [[Uiswitch alloc] Initwithframe:cgrectmake (50, 100, 20, 10)];
[Switchbutton Seton:yes];
[Switchbutton addtarget:self Action: @selector (switchaction:) forcontrolevents:uicontroleventvaluechanged];
[Self.view Addsubview:switchbutton];
Do no additional setup after loading the view, typically from a nib.
}
Switchaction in Selector in code: we need to implement it ourselves, which is the event received when we press.
Remember to add Switchbutton to the current view and call [Self.viewaddSubview:switchButton];
3, monitor Uiswitch Press the event
The implementation code is as follows:
Copy Code code as follows:
-(void) Switchaction: (ID) sender
{
Uiswitch *switchbutton = (uiswitch*) sender;
BOOL Isbuttonon = [Switchbutton ISON];
if (Isbuttonon) {
Showswitchvalue.text = @ "yes";
}else {
Showswitchvalue.text = @ "No";
}
}
Showswitchvalue is what I do by dragging the control method onto the label on the interface to facilitate the display effect
Run, Effect:
Using Uiswitch by drag-and-drop method
1. Drag a Uiswitch control to the Xib file.
2, press Alt+command + Return key to open Assistant editor mode, select the Uiswitch control, press and hold, and drag to ViewController.h
3. Select action Mode
4. m file to implement Switchaction. Just the dynamic creation of the time also used this method name, you can first comment out just.
Copy Code code as follows:
-(Ibaction) Switchaction: (ID) Sender {
Uiswitch *switchbutton = (uiswitch*) sender;
BOOL Isbuttonon = [Switchbutton ISON];
if (Isbuttonon) {
Showswitchvalue.text = @ "yes";
}else {
Showswitchvalue.text = @ "No";
}
}
Here we look at the source code for the. m file:
Copy Code code as follows:
#import "HMCUSTOMSWITCH.H"
@implementation Hmcustomswitch
@synthesize on;
@synthesize Tintcolor, Clippingview, Leftlabel, Rightlabel;
+ (Hmcustomswitch *) Switchwithlefttext: (NSString *) lefttext andright: (NSString *) righttext
{
Hmcustomswitch *switchview = [[Hmcustomswitch alloc] Initwithframe:cgrectzero];
SwitchView.leftLabel.text = Lefttext;
SwitchView.rightLabel.text = Righttext;
return [SwitchView autorelease];
}
-(ID) initWithFrame: (cgrect) rect
{
if ((Self=[super initwithframe:cgrectmake (rect.origin.x,rect.origin.y,95,27)))
{
Self.clipstobounds = YES;
[Self awakefromnib]; Do all setup in awakefromnib so control can is created manually or in a nib file
}
return self;
}
-(void) awakefromnib
{
[Super awakefromnib];
Self.backgroundcolor = [Uicolor Clearcolor];
[Self setthumbimage:[uiimage imagenamed:@ "switchthumb.png"] forstate:uicontrolstatenormal];
[Self setminimumtrackimage:[uiimage imagenamed:@ "switchbluebg.png"] forstate:uicontrolstatenormal];
[Self setmaximumtrackimage:[uiimage imagenamed:@ "switchoffplain.png"] forstate:uicontrolstatenormal];
Self.minimumvalue = 0;
Self.maximumvalue = 1;
self.continuous = NO;
Self.on = NO;
Self.value = 0.0;
Self.clippingview = [[UIView alloc] Initwithframe:cgrectmake (4,2,87,23)];
Self.clippingView.clipsToBounds = YES;
self.clippingView.userInteractionEnabled = NO;
Self.clippingView.backgroundColor = [Uicolor Clearcolor];
[Self addSubview:self.clippingView];
[Self.clippingview release];
NSString *leftlabeltext = nslocalizedstring (@ "on", "Custom uiswitch on label." If localized to empty string then I/O would be used ");
if ([leftlabeltext length] = = 0)
{
Leftlabeltext = @ "L"; Use Helvetica lowercase L to be a 1.
}
Self.leftlabel = [[Uilabel alloc] init];
Self.leftLabel.frame = CGRectMake (0, 0, 48, 23);
Self.leftLabel.text = Leftlabeltext;
Self.leftLabel.textAlignment = Nstextalignmentcenter;
Self.leftLabel.font = [Uifont boldsystemfontofsize:17];
Self.leftLabel.textColor = [Uicolor Whitecolor];
Self.leftLabel.backgroundColor = [Uicolor Clearcolor];
Self.leftLabel.shadowColor = [Uicolor Redcolor];
Self.leftLabel.shadowOffset = Cgsizemake (0,0);
[Self.clippingview AddSubview:self.leftLabel];
[Self.leftlabel release];
NSString *rightlabeltext = nslocalizedstring (@ "Off", "Custom uiswitch off label. If localized to empty string then I/O would be used ");
if ([rightlabeltext length] = = 0)
{
Rightlabeltext = @ "O"; Use Helvetica uppercase O to be a 0.
}
Self.rightlabel = [[Uilabel alloc] init];
Self.rightLabel.frame = CGRectMake (95, 0, 48, 23);
Self.rightLabel.text = Rightlabeltext;
Self.rightLabel.textAlignment = Nstextalignmentcenter;
Self.rightLabel.font = [Uifont boldsystemfontofsize:17];
Self.rightLabel.textColor = [Uicolor Graycolor];
Self.rightLabel.backgroundColor = [Uicolor Clearcolor];
Self.rightLabel.shadowColor = [Uicolor Redcolor];
Self.rightLabel.shadowOffset = Cgsizemake (0,0);
[Self.clippingview AddSubview:self.rightLabel];
[Self.rightlabel release];
}
-(void) layoutsubviews
{
[Super Layoutsubviews];
NSLog (@ "leftlabel=%@", Nsstringfromcgrect (Self.leftLabel.frame));
Move the labels to the front
[Self.clippingview Removefromsuperview];
[Self addSubview:self.clippingView];
CGFloat thumbwidth = self.currentThumbImage.size.width;
CGFloat switchwidth = self.bounds.size.width;
CGFloat labelwidth = switchwidth-thumbwidth;
CGFloat inset = self.clippingview.frame.origin.x;
Nsinteger xpos = Self.value * (self.bounds.size.width-thumbwidth)-(SELF.LEFTLABEL.FRAME.SIZE.WIDTH-THUMBWIDTH/2);
Nsinteger xpos = Self.value * LABELWIDTH-LABELWIDTH-INSET;
Self.leftLabel.frame = CGRectMake (xpos, 0, Labelwidth, 23);
Xpos = Self.value * (self.bounds.size.width-thumbwidth) + (SELF.RIGHTLABEL.FRAME.SIZE.WIDTH-THUMBWIDTH/2);
Xpos = Switchwidth + (Self.value * labelwidth-labelwidth)-inset;
Self.rightLabel.frame = CGRectMake (xpos, 0, Labelwidth, 23);
NSLog (@ "value=%f xpos=%i", Self.value,xpos);
NSLog (@ "thumbwidth=%f self.bounds.size.width=%f", thumbwidth,self.bounds.size.width);
}
-(UIImage *) Image: (uiimage*) Image tintedwithcolor: (Uicolor *) tint
{
if (tint!= nil)
{
Uigraphicsbeginimagecontext (image.size);
Draw mask so the alpha is respected
Cgcontextref CurrentContext = Uigraphicsgetcurrentcontext ();
Cgimageref maskimage = [Image cgimage];
Cgcontextcliptomask (CurrentContext, cgrectmake (0, 0, Image.size.width, image.size.height), maskimage);
Cgcontextdrawimage (CurrentContext, CGRectMake (0,0, Image.size.width, image.size.height), image. Cgimage);
[Image Drawatpoint:cgpointmake (0,0)];
[Tint Setfill];
Uirectfillusingblendmode (CGRectMake (0,0,image.size.width,image.size.height), kcgblendmodecolor);
UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
return newimage;
}
Else
{
return image;
}
}
-(void) Settintcolor: (uicolor*) color
{
if (color!= tintcolor)
{
[Tintcolor release];
Tintcolor = [color retain];
[Self setminimumtrackimage:[self image:[uiimage imagenamed:@ "Switchbluebg.png"] tintedwithcolor:tintcolor] ForState : UIControlStateNormal];
}
}
-(void) SetOn: (BOOL) Turnon animated: (bool) animated;
{
on = Turnon;
if (animated)
{
[UIView Beginanimations:nil Context:nil];
[UIView setanimationduration:0.2];
}
if (ON)
{
Self.value = 1.0;
}
Else
{
Self.value = 0.0;
}
if (animated)
{
[UIView commitanimations];
}
}
-(void) SetOn: (BOOL) Turnon
{
[Self Seton:turnon animated:no];
}
-(void) Endtrackingwithtouch: (Uitouch *) Touch withevent: (uievent *) event
{
NSLog (@ "Preendtrackingwithtouch");
[Super Endtrackingwithtouch:touch Withevent:event];
NSLog (@ "Postendtrackingwithtouch");
M_touchedself = YES;
[Self seton:on animated:yes];
}
-(void) Touchesbegan: (nsset*) touches withevent: (uievent*) event
{
[Super Touchesbegan:touches Withevent:event];
NSLog (@ "Touchesbegan");
M_touchedself = NO;
on =!on;
}
-(void) touchesended: (nsset*) touches withevent: (uievent*) event
{
[Super Touchesended:touches Withevent:event];
NSLog (@ "touchesended");
if (!m_touchedself)
{
[Self seton:on animated:yes];
[Self sendactionsforcontrolevents:uicontroleventvaluechanged];
}
}
-(void) dealloc
{
[Tintcolor release];
[Clippingview release];
[Rightlabel release];
[Leftlabel release];
[Super Dealloc];
}
@end
Look at the code can know, in fact, it is through the inheritance of UISlider control, UISlider is a uilabel, when yes, the slider slipped to the far right, no when the slide to the left.
So use it in your code? Here's another example:
Copy Code code as follows:
-(void) Loadview
{
UIView *contentview = [[UIView alloc] Initwithframe:[[uiscreen Mainscreen] applicationframe]];
Self.view = Contentview;
Contentview.backgroundcolor = [Uicolor Whitecolor];
Standard On/off
Hmcustomswitch *switchview = [[Hmcustomswitch alloc] Initwithframe:cgrectzero];
Switchview.center = Cgpointmake (160.0f, 20.0f);
Switchview.on = YES;
[Contentview Addsubview:switchview];
[SwitchView release];
Custom yes/no
SwitchView = [Hmcustomswitch switchwithlefttext:@ "YES" andright:@ "NO"];
Switchview.center = Cgpointmake (160.0f, 60.0f);
Switchview.on = YES;
[Contentview Addsubview:switchview];
Custom Font and color
SwitchView = [Hmcustomswitch switchwithlefttext:@ "Hello" andright:@ "ABC"];
Switchview.center = Cgpointmake (160.0f, 100.0f);
Switchview.on = YES;
[Switchview.leftlabel Setfont:[uifont boldsystemfontofsize:13.0f]];
[Switchview.rightlabel Setfont:[uifont italicsystemfontofsize:15.0f]];
[Switchview.rightlabel Settextcolor:[uicolor Bluecolor]];
[Contentview Addsubview:switchview];
Multiple lines
SwitchView = [Hmcustomswitch switchwithlefttext:@ "Hello\nworld" andright:@ "Bye\nworld"];
Switchview.center = Cgpointmake (160.0f, 140.0f);
Switchview.on = YES;
Switchview.tintcolor = [Uicolor Orangecolor];
SwitchView.leftLabel.font = [Uifont boldsystemfontofsize:9.0f];
SwitchView.rightLabel.font = [Uifont boldsystemfontofsize:9.0f];
SwitchView.leftLabel.numberOfLines = 2;
SwitchView.rightLabel.numberOfLines = 2;
SwitchView.leftLabel.lineBreakMode = nslinebreakbywordwrapping;
SwitchView.rightLabel.lineBreakMode = nslinebreakbywordwrapping;
[Contentview Addsubview:switchview];
SwitchView = [[Hmcustomswitch alloc] init];
Switchview.center = Cgpointmake (160.0f, 180.0f);
Switchview.on = YES;
Switchview.tintcolor = [Uicolor Purplecolor];
[Contentview Addsubview:switchview];
[SwitchView release];
SwitchView = [Hmcustomswitch switchwithlefttext:@ "L" andright:@ "O"];
Switchview.center = Cgpointmake (160.0f, 220.0f);
Customswitch.tintcolor = [Uicolor colorwithred:125.f/255.f green:157.f/255.f blue:93.f/255.f alpha:1.0];
Customswitch.tintcolor = [Uicolor colorwithred:125.f/255.f green:157.f/255.f blue:93.f/255.f alpha:1.0];
[Contentview Addsubview:switchview];
Standard On/off
SwitchView = [[Hmcustomswitch alloc] init];
Switchview.center = Cgpointmake (160.0f, 260.0f);
Switchview.tintcolor = [Uicolor colorwithred:125.f/255.f green:157.f/255.f blue:93.f/255.f alpha:1.0];
[SwitchView addtarget:self Action: @selector (switchflipped:) forcontrolevents:uicontroleventvaluechanged];
[Contentview Addsubview:switchview];
[SwitchView release];
Uitoolbar *toolbar = [[Uitoolbar alloc] Initwithframe:cgrectmake (0, 420, 320, 40)];
Toolbar.tintcolor = [Uicolor colorwithred:125.f/255.f green:157.f/255.f blue:93.f/255.f alpha:1.0];
[Contentview Addsubview:toolbar];
[Contentview release];
}
-(void) switchflipped: (hmcustomswitch*) SwitchView
{
NSLog (@ "switchflipped=%f on:%@", Switchview.value, (switchview.on?@ "Y": @ "N"));
}