自定义UISwitch

ViewController.m

 1 #import "ViewController.h"

 2 #import "MyUISwitch.h"

 3 @interface ViewController ()

 4             

 5 

 6 @end

 7 

 8 @implementation ViewController

 9             

10 - (void)viewDidLoad {

11     [super viewDidLoad];

12     

13     UISwitch* sw = [[UISwitch alloc]initWithFrame:CGRectMake(200, 100, 0, 0)];

14     [self.view addSubview:sw];

15     

16     MyUISwitch* s = [[MyUISwitch alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];

17     [self.view addSubview:s];

18     

19     

20 }

MyUISwitch.h

1 #import <UIKit/UIKit.h>

2 

3 @interface MyUISwitch : UIControl

4 @property (nonatomic,assign,getter=isOn) BOOL on;

5 @end

MyUISwitch.m

 1 #import "MyUISwitch.h"

 2 

 3 @implementation MyUISwitch

 4 

 5 - (instancetype)init{

 6     return [self initWithFrame:CGRectZero];

 7 }

 8 

 9 - (instancetype)initWithFrame:(CGRect)frame

10 {

11     frame.size.width = 49;

12     frame.size.height = 31;

13     self = [super initWithFrame:frame];

14     if (self) {

15         UIView* backView = [[UIView alloc]initWithFrame:self.bounds];

16         backView.backgroundColor = [UIColor greenColor];

17         backView .tag = 99;

18         backView.layer.cornerRadius = 15.5;

19         [self addSubview:backView];

20         

21         CGRect r = self.bounds;

22         r.origin.x = .5;

23         r.origin.y = .5;

24         r.size.height -= 1;

25         r.size.width -= .5;

26         UIView* offBackView = [[UIView alloc]initWithFrame:r];

27         offBackView.backgroundColor = [UIColor whiteColor];

28         offBackView.tag = 100;

29         offBackView.layer.cornerRadius = 16;

30         [self addSubview:offBackView];

31         

32         UIView* thumb = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];

33         thumb.layer.cornerRadius = 15;

34         thumb.tag= 101;

35         thumb.backgroundColor = [UIColor whiteColor];

36         thumb.layer.borderWidth = 0.5;

37         thumb.layer.borderColor = [UIColor grayColor].CGColor;

38         [self addSubview:thumb];

39     }

40     return self;

41 }

42 -(void)setFrame:(CGRect)frame{

43     frame.size.width = 49;

44     frame.size.height = 31;

45     [super setFrame:frame];

46 }

47 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

48      // thumb frame

49 }

50 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

51     // UITouch *touch = [touches anyObject];

52     // CGPoint p = [touch locationInView:self];

53     _on =! _on;

54     UIView* thumb = [self viewWithTag:101];

55     UIView* back = [self viewWithTag:99];

56     UIView* off = [self viewWithTag:100];

57     

58     [UIView animateWithDuration:.2 animations:^{

59     if (_on) {

60         thumb.center = CGPointMake(self.frame.size.width-thumb.frame.size.width/2,thumb.center.y);

61     }else{

62         back.backgroundColor = [UIColor greenColor];

63         thumb.center = CGPointMake(thumb.frame.size.width/2, thumb.center.y);

64     }}];

65     off.hidden= _on;

66 }

67 

68 @end

 

你可能感兴趣的:(switch)