iOS中自定义event原理分析与使用步骤
2014-12-17 10:13
295 查看
本文由DevDiv Vincent (vincent@devdiv.com)原创,转载请联系作者!
最近DevDiv论坛上有网友在问如何自定义event,
自定义event在其他平台,如Windows Phone上实现自定义控件是很常见的,其实在iOS中也是支持自定义event的。
原理分析如下:
1. 我们先来看看自定义消息id:
我们来看看UIControl的定义,我们打算自定义一个control,继承自它
在UIControl中大家可以看到以下enum:
其中我们可以通过UIControlEventApplicationReserved自定义一个event(实际上event就是一个数字,或者叫消息id)
如果你把这个十六进制表示改为二级制那么,它的值为0x0F000000 = 00001111 00000000 00000000 00000000
这个定义的意思是表示有4位1111是为我们保留可以自定义消息的,所以我们能够自定义的消息id依次为
2. 消息action注册
在iOS中我们一般通过addTarget:action:forControlEvents:注册一个event的处理函数
A control maintains an internal dispatch table: for each control event there is some number of target-action pairs, of which the action is a selector (the name of a method) and the target is the object to which that message is to be sent. When a control
event occurs, the control consults its dispatch table, finds all the targer-action pairs associated with that control event, and sends each action message to the corresponding target.
也就是说控件内部有一个dispatch table,维护了event和action对应关系。
我们调用addTarget:action:forControlEvents:时候就会在这张表中增加一条记录
3. 消息触发
我们可以通过UIControl的sendActionsForControlEvents:触发一个event
一旦évent触发,那么ta它会查找dispatch table,调用相应的action
代码示例:
1. 我们先创建一个DevDivCustomEvent的工程
创建一个CustomControl,继承在UIControl;并定义一个自定义消息UIControlEventCustom1,代码如下
2. 在ViewController.h中声明一个CustomControl属性
3. 打开ViewController.m文件,在viewDidLoad中创建CustomControl的实例,并为UIControlEventTouchUpInside和UIControlEventCustom1注册action,代码如下:
4. 在customControlTouchUpInside:触发UIControlEventCustom1,代码如下:
5. 如果customControlCustom1Handler:能够执行,说明注册event action和trigger event都成功了,代码如下:
6. 执行工程,可以发现第5步中的Log可以打印出来,效果如图:
7. 代码下载:
http://www.devdiv.com/iOS_iPhone-ios_event_-thread-203125-1-1.html
最近DevDiv论坛上有网友在问如何自定义event,
自定义event在其他平台,如Windows Phone上实现自定义控件是很常见的,其实在iOS中也是支持自定义event的。
原理分析如下:
1. 我们先来看看自定义消息id:
我们来看看UIControl的定义,我们打算自定义一个control,继承自它
在UIControl中大家可以看到以下enum:
[代码]c#/cpp/oc代码:
1 | UIControlEventAllTouchEvents = 0x00000FFF, |
2 | UIControlEventAllEditingEvents= 0x000F0000, |
3 | UIControlEventApplicationReserved = 0x0F000000, |
4 | UIControlEventSystemReserved = 0xF0000000, |
5 | UIControlEventAllEvents = 0xFFFFFFFF |
如果你把这个十六进制表示改为二级制那么,它的值为0x0F000000 = 00001111 00000000 00000000 00000000
这个定义的意思是表示有4位1111是为我们保留可以自定义消息的,所以我们能够自定义的消息id依次为
[代码]c#/cpp/oc代码:
1 | 0x00000001<<27 = 00001000 00000000 00000000 00000000 |
2 | 0x00000001<<26 = 00000100 00000000 00000000 00000000 |
3 | 0x00000001<<25 = 00000010 00000000 00000000 00000000 |
4 | 0x00000001<<24 = 00000001 00000000 00000000 00000000 |
在iOS中我们一般通过addTarget:action:forControlEvents:注册一个event的处理函数
A control maintains an internal dispatch table: for each control event there is some number of target-action pairs, of which the action is a selector (the name of a method) and the target is the object to which that message is to be sent. When a control
event occurs, the control consults its dispatch table, finds all the targer-action pairs associated with that control event, and sends each action message to the corresponding target.
也就是说控件内部有一个dispatch table,维护了event和action对应关系。
我们调用addTarget:action:forControlEvents:时候就会在这张表中增加一条记录
3. 消息触发
我们可以通过UIControl的sendActionsForControlEvents:触发一个event
一旦évent触发,那么ta它会查找dispatch table,调用相应的action
代码示例:
1. 我们先创建一个DevDivCustomEvent的工程
创建一个CustomControl,继承在UIControl;并定义一个自定义消息UIControlEventCustom1,代码如下
[代码]c#/cpp/oc代码:
01 | // |
02 | // CustomControl.h |
03 | // DevDivCustomEvent |
04 | // |
05 | // Created by Vincent on 13-5-31. |
06 | // Copyright (c) 2013年 DevDiv Community. All rights reserved. |
07 | // |
08 |
09 | #import <UIKit/UIKit.h> |
10 |
11 | enum { |
12 | UIControlEventCustom1 = 0x00000001<<25, |
13 | }; |
14 |
15 | @ interface CustomControl : UIControl |
16 |
17 | @end |
[代码]c#/cpp/oc代码:
1 | @property (nonatomic, strong) CustomControl *customControl; |
[代码]c#/cpp/oc代码:
01 | - ( void )viewDidLoad |
02 | { |
03 | [super viewDidLoad]; |
04 | // Do any additional setup after loading the view, typically from a nib. |
05 |
06 | /* Make sure our view is white */ |
07 | self.view.backgroundColor = [UIColor whiteColor]; |
08 |
09 | /* Create the custom control */ |
10 | self.customControl = [[CustomControl alloc] initWithFrame: |
11 | CGRectMake(100, 100, 100, 100)]; |
12 | self.customControl.backgroundColor = [UIColor greenColor]; |
13 |
14 | // Register event action selector |
15 | [self.customControl addTarget:self action:@selector(customControlTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; |
16 |
17 | [self.customControl addTarget:self action:@selector(customControlCustom1Handler:) forControlEvents:UIControlEventCustom1]; |
18 |
19 | [self.view addSubview:self.customControl]; |
20 | } |
[代码]c#/cpp/oc代码:
1 | // Trigger custom event |
2 | - ( void )customControlTouchUpInside:(CustomControl*)paramSender |
3 | { |
4 | [paramSender sendActionsForControlEvents:UIControlEventCustom1]; |
5 | } |
[代码]c#/cpp/oc代码:
1 | - ( void )customControlCustom1Handler:(CustomControl*)paramSender |
2 | { |
3 | NSLog( @"DevDiv.com Custom Event Triggered!" ); |
4 | } |
7. 代码下载:
http://www.devdiv.com/iOS_iPhone-ios_event_-thread-203125-1-1.html
相关文章推荐
- iOS中如何自定义加密缓存及缓存原理分析(第二节:自定义缓存类)
- IOS WebViewJavascriptBridge 使用以及原理分析
- iOS使用的一些库 原理分析
- iOS中如何自定义加密缓存及缓存原理分析(第一节:缓存原理分析)
- EventBus原理源码分析和使用
- IOS对象关系映射(ORM)之coreData框架的学习——(二)原理剖析及使用步骤详解
- iOS开发:runtime原理简单分析和基本使用场景
- IOS自定义NSLOG和断言的使用/description方法的使用原理(对象,数组,字典)
- [原创] 使用UML做需求分析的步骤
- SD--对于定价过程参考步骤 (Condition Step)(T683S-STUNB and T683S-STUN2)的使用代码分析
- SD--对于定价过程参考步骤 (Condition Step)(T683S-STUNB and T683S-STUN2)的使用代码分析
- 【华磊随笔】善于使用工具(1)-使用IE8开发人员工具和FireBug分析Jquery FlexiGrid的实现原理
- struts对comments-beanutils包的使用分析以及使用自定义类型转化器的ActionServlet
- [EntLib]微软企业库5.0 学习之路——第七步、Cryptographer加密模块简单分析、自定义加密接口及使用—下篇
- OAuth认证协议原理分析及使用方法
- 微软企业库5.0 学习之路——第七步、Cryptographer加密模块简单分析、自定义加密接口及使用—上篇
- 微软企业库5.0 学习之路——第七步、Cryptographer加密模块简单分析、自定义加密接口及使用—下篇
- 编译原理之LL(1)文法分析(使用分析表)
- Lighttpd1.4.20源码分析之fdevent系统(3) -----使用
- JNI技术实践小结--原理分析和详细步骤截图说明