Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc?viewmode=contents
In the afternoon idle bored, simply think, with three uilabel to achieve this simple custom uiswitch
,
Of course, some rough, follow-up time I will optimize the interface. Directly take to use the estimated interface rough point, there are many source code on the Internet. After writing so many blog found, actually learn to think very important, here hope, let the students want to learn to know how to customize a control.
The first step is to analyze the constituent elements of the custom control
In my case, it's a four-part.
1. Left Half view-uilabel
2. Right half view-uilabel
3. Slide the View-uilabel
4. Add gestures to the sliding view-uitapgesturerecognizer
Step two, design the interface
In my case, the interface is relatively simple, that is, the state of the control, denoted by bool
So the header file is
#import <UIKit/UIKit.h> @interface hwccustomuiswitch:uiview@property (nonatomic) BOOL isOn; @end
The third step, design and implementation
1. Lazy initialization of three labels in getter, gesture recognition for sliding view
2. UI synchronization in the setter function of ISON to ensure that each model changes, view changes (MVC mode)
The. m file is
hwccustomuiswitch.m//customactivityindicator////Created by Huangwenchen on 15/1/5.//Copyright (c) 2015 BLOGFO Rcsdn. All rights reserved.//#import "HWCCUSTOMUISWITCH.H" @interface Hwccustomuiswitch () @property (strong,nonatomic) UILabel * Leftlabel; @property (strong,nonatomic) UILabel * Rightlabel; @property (strong,nonatomic) UILabel * taglabel;@ End@implementation hwccustomuiswitch-(uilabel*) taglabel{if (!_taglabel) {CGRect frame = Self.ison? CGRectMake (0, 0,SELF.FRAME.SIZE.WIDTH/2, self.frame.size.height): CGRectMake (self.frame.size.width/2,0, Self.frame.size.width/2,self.frame.size.height); _taglabel = [[UILabel alloc] initwithframe:frame]; } return _taglabel;} -(uilabel*) leftlabel{if (!_leftlabel) {CGRect frame = CGRectMake (0, 0, SELF.FRAME.SIZE.WIDTH/2, Self.frame.siz E.height); _leftlabel = [[UILabel alloc] initwithframe:frame]; _leftlabel.text = @ "on"; _leftlabel.textalignment = Nstextalignmentcenter; } return _leftlabel;} -(uilabel*) rightlabel{if (!_rightlabel) {CGRect frame = CGRectMake (self.frame.size.width/2,0, Self.frame.size. WIDTH/2, Self.frame.size.height); _rightlabel = [[UILabel alloc] initwithframe:frame]; _rightlabel.text = @ "OFF"; _rightlabel.textalignment = Nstextalignmentcenter; } return _rightlabel;} -(void) Setison: (BOOL) ison{cgfloat width = self.frame.size.width; CGFloat height = self.frame.size.height; _ison = IsOn; if (isOn) {[UIView animatewithduration:1.0 delay:0.01 Opti Ons:uiviewanimationoptioncurveeaseinout animations:^{Self.tagLabel.ce nter = Cgpointmake (WIDTH/4,HEIGHT/2); } completion:^ (BOOL finished) {}]; }else{[UIView animatewithduration:1.0 delay:0.01 Options: Uiviewanimationoptioncurveeaseinout animations:^{Self.tagLabel.cente R = Cgpointmake (3*WIDTH/4,HEIGHT/2); } completion:^ (BOOL finished) {}]; }}-(void) setup{UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (TA p)]; [Self.taglabel Addgesturerecognizer:tap]; self.tagLabel.userInteractionEnabled = YES; self.userinteractionenabled = YES; Self.rightLabel.backgroundColor = [Uicolor Browncolor]; [Self AddSubview:self.rightLabel]; Self.leftLabel.backgroundColor = [Uicolor Yellowcolor]; [Self AddSubview:self.leftLabel]; Self.tagLabel.backgroundColor = [Uicolor Greencolor]; [Self AddSubview:self.tagLabel]; Self.layer.cornerRadius = SELF.FRAME.SIZE.HEIGHT/10; Self.clipstobounds = YES;} -(void) tap{Self.ison =!self.ison;} -(ID) Initwithcoder: (Nscoder *) adecoder{if (self = [supeR Initwithcoder:adecoder]) {[Self setUp]; } return self;} -(ID) initWithFrame: (CGRect) frame{if (self = [Super Initwithframe:frame]) {[Self setUp]; } return self;} @endThen, in the place of use
Hwccustomuiswitch * Hwcswitch = [[Hwccustomuiswitch alloc] Initwithframe:cgrectmake (+, +, +)]; [Self.view Addsubview:hwcswitch];
If you want to use the image to set the properties of the Uilabel directly, it's OK.
iOS Custom Uiswitch