iOS中自定义event原理分析与使用步骤
来源:互联网 发布:mmd怎么制作动作数据 编辑:程序博客网 时间:2024/05/10 20:58
本文由DevDiv Vincent (vincent@devdiv.com)原创,转载请联系作者!
最近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
其中我们可以通过UIControlEventApplicationReserved自定义一个event(实际上event就是一个数字,或者叫消息id)
如果你把这个十六进制表示改为二级制那么,它的值为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
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,代码如下
[代码]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
2. 在ViewController.h中声明一个CustomControl属性
[代码]c#/cpp/oc代码:
1
@property (nonatomic, strong) CustomControl *customControl;
3. 打开ViewController.m文件,在viewDidLoad中创建CustomControl的实例,并为UIControlEventTouchUpInside和UIControlEventCustom1注册action,代码如下:
[代码]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
}
4. 在customControlTouchUpInside:触发UIControlEventCustom1,代码如下:
[代码]c#/cpp/oc代码:
1
// Trigger custom event
2
- (
void
)customControlTouchUpInside:(CustomControl*)paramSender
3
{
4
[paramSender sendActionsForControlEvents:UIControlEventCustom1];
5
}
5. 如果customControlCustom1Handler:能够执行,说明注册event action和trigger event都成功了,代码如下:
[代码]c#/cpp/oc代码:
1
- (
void
)customControlCustom1Handler:(CustomControl*)paramSender
2
{
3
NSLog(
@"DevDiv.com Custom Event Triggered!"
);
4
}
6. 执行工程,可以发现第5步中的Log可以打印出来,效果如图:
7. 代码下载:
http://www.devdiv.com/iOS_iPhone-ios_event_-thread-203125-1-1.html
0 0
- iOS中自定义event原理分析与使用步骤
- iOS中自定义步骤进度条
- .Net中自定义事件的步骤与使用
- .Net中自定义事件的步骤与使用
- 在Android中使用TensorFlow的原理与步骤
- iOS中如何自定义加密缓存及缓存原理分析(第一节:缓存原理分析)
- View自定义属性步骤与分析
- iOS中如何自定义加密缓存及缓存原理分析(第二节:自定义缓存类)
- flex中 event.target与 event.currentTarget的使用
- gem5中event queue运行原理机制详细分析
- JavaScript中Apply和Call原理分析与使用
- spring中自定义Event事件的使用和浅析
- AIDL原理分析与使用
- Android AsyncTask使用步骤与源码分析
- miranda中Service与Event机制分析结果
- iOS中使用自定义字体
- ios中使用百度地图简单步骤
- iOS 中 coredata 的使用步骤
- 视图控制器的生命周期
- FFPLAY的原理(三)
- MySQL数据库中对于网卡绑定对授权和访问的影响测试一例
- Android内存问题的几种解决方案(二)
- Install Redmine on Centos 6.5 - 64 bit
- iOS中自定义event原理分析与使用步骤
- 人因梦想而伟大。有梦最美,希望相随
- jQuery扩展命名空间(扩展方法)
- Android随笔之——PackageManager详解
- FFPLAY的原理(四)
- Memcached基础
- Powershell Mail module, 发送outbox 里的所有邮件(一个.csv文件代表一封邮件)
- FFPLAY的原理(五)
- CSS的选择器 和 盒子模型