[Code Note] iOS-custom switch, ios-custom switch
I ,.
2. Engineering Drawing.
3. Code.
ViewController. h
#import <UIKit/UIKit.h>#import "CustomSwitch.h"@interface ViewController : UIViewController@property (nonatomic, strong) CustomSwitch * leftSwitch;@property (nonatomic, strong) CustomSwitch * rightSwitch;@end
ViewController. m
# Import "ViewController. h "# import" CustomSwitch. h "# define TAG_COLOR BOTTOM_CLICK_COLOR # define BOTTOM_CLICK_COLOR (41,115,192, 1) # define Color (r, g, B, d) [UIColor colorWithRed :( r)/255.0 green :( g) /255.0 blue :( B)/255.0 alpha: d] @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // initialize the selection box page [self addSwitchView];} # pragma-mark-functions // initialize the selection box page-(void) addSwitchView {CustomSwitch * leftSwitch = [[CustomSwitch alloc] initWithFrame: CGRectMake (20,100,200, 50) onColor: TAG_COLOR offColor: Color (214,214,214, 1) font: [UIFont systemFontOfSize: 15] ballSize: 25]; leftSwitch. onText = @ "Technical Support"; leftSwitch. offText = @ "no technical support"; leftSwitch. userInteractionEnabled = YES; leftSwitch. on = NO; leftSwitch. tag = 0; [leftSwitch addTarget: self action: @ selector (handleTapLeftSwitch :) forControlEvents: UIControlEventValueChanged]; [self. view addSubview: leftSwitch]; CustomSwitch * rightSwitch = [[CustomSwitch alloc] usage: CGRectMake (20,200,200, 50) onColor: TAG_COLOR offColor: Color (214,214,214, 1) font: [UIFont systemFontOfSize: 15] ballSize: 25]; rightSwitch. onText = @ "resolved"; rightSwitch. offText = @ "unsolved"; rightSwitch. userInteractionEnabled = YES; rightSwitch. on = NO; leftSwitch. tag = 1; [rightSwitch addTarget: self action: @ selector (handleTapRightSwitch :) forControlEvents: UIControlEventValueChanged]; [self. view addSubview: rightSwitch] ;}# pragma-mark-functions // click the event-(void) handleTapLeftSwitch (CustomSwitch *) on the left *) customSwitch {NSLog (@ "Click Event on the left");} // Click Event-(void) handleTapRightSwitch (CustomSwitch *) on the Right *) customSwitch {NSLog (@ "Click Event on the right");}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end