10.16 UISwitch练习

今天仿照老师自定义的UISwitch版本,自己也自定义了一个,监听UIControlEventValueChanged事件。

老师讲的是用协议和代理来传递该事件的处理,布置的练习是用block对象来代替代理实现,自己尝试着写了

一个版本。代码如下:

自定义的UISwitch类的头文件:

 1 // MySwitch.h
 2 #import <UIKit/UIKit.h>
 3 
 4 @class MySwitch;
 5 
 6 //协议
 7 @protocol MySwitchDelegate <NSObject>
 8 
 9 - (void)didMySwitchClicked:(MySwitch *)sender;
10 
11 @end
12 
13 @interface MySwitch : UIView
14 
15 @property (nonatomic, assign) BOOL on; //开关属性
16 @property (nonatomic, assign) id<MySwitchDelegate> delegate; //委托
17 @property (nonatomic, assign) void(^block)(MySwitch *sender);//block对象
18 @end

自己实现的UISwitch类的实现文件:

 1 // MySwitch.m
 2 #import "MySwitch.h"
 3 
 4 @interface MySwitch ()
 5 {
 6     UIView *_thumbView; //控制开关的滑块
 7 }
 8 @end
 9 
10 @implementation MySwitch
11 
12 - (instancetype)initWithFrame:(CGRect)frame
13 {
14     if (self = [super initWithFrame:frame]) {
15         _on = NO;
16         _thumbView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 2, frame.size.height)];
17         _thumbView.backgroundColor = [UIColor greenColor];
18         [self addSubview:_thumbView];
19     }
20     return self;
21 }
22 
23 //监听触摸开始事件
24 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
25 {
26     [UIView animateWithDuration:0.1 animations:^{
27         if(self.on){
28             //开关原先为YES时,移动开关到左边
29             _thumbView.frame = CGRectMake(0, 0, self.frame.size.width / 2, self.frame.size.height);
30             self.backgroundColor = [UIColor grayColor];
31             self.on = NO;
32         }else{
33             //开关原先为NO时,移动开关到右边。
34             _thumbView.frame = CGRectMake(self.frame.size.width / 2, 0, self.frame.size.width / 2, self.frame.size.height);
35             self.backgroundColor = [UIColor yellowColor];
36             self.on = YES;
37         }
38     } completion:^(BOOL finished){
39         //通过委托的方式调用
40 //        if(_delegate && [_delegate respondsToSelector:@selector(didMySwitchClicked:)]){
41 //            [_delegate didMySwitchClicked:self];
42 //        };
43         //通过block对象的方式调用
44         if(_block){
45             _block(self);
46         }
47     }];
48 }
49 @end


viewController.m文件中加载自己实现的UISwitch版本:

 1 // viewController.m 
 2 - (void)viewDidLoad {
 3     [super viewDidLoad];
 4     
 5     //UISwitch
 6     UISwitch *s = [[UISwitch alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
 7     //设置本对象监听到的指定事件交给哪个对象已经该对象的哪个方法处理。
 8     [s addTarget:self action:@selector(didSwitchClicked:) forControlEvents:UIControlEventValueChanged];
 9     
10     [self.view addSubview:s];
11     
12     //自己实现的UISwitch版本
13     MySwitch *mySwitch = [[MySwitch alloc] initWithFrame:CGRectMake(200, 50, 100, 50)];
14     mySwitch.backgroundColor = [UIColor yellowColor];
15     //mySwitch.delegate = self;//设置委托对象。

16 void(^block)(MySwitch *sender) = ^(MySwitch *sender) 17 { 18 NSLog(@"%@", sender.on ? @"NO" : @"YES"); 19 }; 20 mySwitch.block = block; //设置block对象。 21 [self.view addSubview:mySwitch]; 22 }

 

 

posted @ 2014-10-16 22:58  _蚕豆_  阅读(162)  评论(0)    收藏  举报