iOS中自定义event原理分析与使用步骤

本文由DevDiv Vincent (vincent@devdiv.com)原创,转载请联系作者!

最近DevDiv论坛上有网友在问如何自定义event,
自定义event在其他平台,如Windows Phone上实现自定义控件是很常见的,其实在iOS中也是支持自定义event的。

原理分析如下:
1. 我们先来看看自定义消息id:
我们来看看UIControl的定义,我们打算自定义一个control,继承自它
在UIControl中大家可以看到以下enum:

[代码]c#/cpp/oc代码:

1UIControlEventAllTouchEvents      = 0x00000FFF,
2UIControlEventAllEditingEvents    = 0x000F0000,
3UIControlEventApplicationReserved = 0x0F000000,
4UIControlEventSystemReserved      = 0xF0000000,
5UIControlEventAllEvents           = 0xFFFFFFFF

其中我们可以通过UIControlEventApplicationReserved自定义一个event(实际上event就是一个数字,或者叫消息id)

如果你把这个十六进制表示改为二级制那么,它的值为0x0F000000 = 00001111 00000000 00000000 00000000
这个定义的意思是表示有4位1111是为我们保留可以自定义消息的,所以我们能够自定义的消息id依次为

[代码]c#/cpp/oc代码:

10x00000001<<27 = 00001000 00000000 00000000 00000000
20x00000001<<26 = 00000100 00000000 00000000 00000000
30x00000001<<25 = 00000010 00000000 00000000 00000000
40x00000001<<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 
11enum {
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. 代码下载:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用SwiftUI自定义iOS分段控件可以通过以下步骤实现: 1. 创建一个新的SwiftUI View,命名为SegmentedControl。 2. 在SegmentedControl定义一个枚举类型,用于表示分段控件的选项。 3. 在SegmentedControl定义一个@Binding属性用于绑定选的选项。 4. 在SegmentedControl使用ForEach循环遍历所有的选项,并将它们显示在分段控件。 5. 在ForEach循环使用Button显示每一个选项,并在按钮的action更新选的选项。 6. 为分段控件添加样式,例如设置选的选项的背景色和字体颜色等。 下面是一个简单的示例代码: ```swift enum SegmentedOption: String, CaseIterable { case option1 case option2 case option3 } struct SegmentedControl: View { @Binding var selectedOption: SegmentedOption var body: some View { HStack { ForEach(SegmentedOption.allCases, id: \.self) { option in Button(action: { self.selectedOption = option }) { Text(option.rawValue) .foregroundColor(self.selectedOption == option ? .white : .black) .padding(.horizontal, 20) .padding(.vertical, 10) .background(self.selectedOption == option ? Color.blue : Color.gray) .cornerRadius(10) } } } } } ``` 在使用时,只需要将SegmentedControl添加到需要显示的View,并将选的选项绑定到某个属性即可。例如: ```swift struct ContentView: View { @State private var selectedOption: SegmentedOption = .option1 var body: some View { VStack { SegmentedControl(selectedOption: $selectedOption) Text("Selected option: \(selectedOption.rawValue)") } } } ``` 这样就可以在界面上显示一个自定义iOS分段控件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值