iOS基础 - 动作目标机制

▶ target-action

目标是动作消息的接收者,一个控件或更为常见的是它的单元,以插座变量的形式保有其他动作消息的目标

动作是控件发送给目标的消息,从目标角度看,它是目标为了响应动作而实现的方法。程序需要某些机制来进行事件和指令的翻译,这个机制就是目标-动作机制

如何使用 target-action?下面代码中会分别采用枚举、动作目标两种方式!采取枚举方式的会在视图 TouchView 内部响应用户操作;而采用动作目标方式的则在目标对象 ViewController 中进行响应

// - TouchView.h

复制代码
 1 #import <UIKit/UIKit.h>
 2 typedef enum{
 3     TouchViewTypeChangeColor,
 4     TouchViewTypeChangeFrame,
 5     TouchViewTypeChangeBounds
 6 }TouchViewType;
 7 
 8 @interface TouchView : UIView{
 9     TouchViewType _type;
10 }
11 
12 // 枚举
13 - (id)initWithFrame:(CGRect)frame type:(TouchViewType )aType;
14 
15 // target-action
16 @property(nonatomic,assign)NSObject *target; // 建议使用 id型
17 @property(nonatomic,assign)SEL action;
18 - (id)initWithTarget:(NSObject *)aTarget action:(SEL)aAction;
19 
20 @end
复制代码

// - TouchView.m

复制代码
 1 #import "TouchView.h"
 2 @implementation TouchView
 3 
 4 // 枚举
 5 - (id)initWithFrame:(CGRect)frame type:(TouchViewType )aType{
 6     self = [super initWithFrame:frame];
 7     if (self) {
 8         _type = aType;
 9     }
10     return self;
11 }
12 
13 // target - action
14 - (id)initWithTarget:(NSObject *)aTarget action:(SEL)aAction{
15     self = [super init];
16     if (self) {
17         _target = aTarget;
18         _action = aAction;
19     }
20     return self;
21 }
22 
23 // 点击视图
24 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
25 
26     //----------------------- 枚举 --------------------------
27     // 我们要清楚 View 的作用只是负责展示内容和响应用户的点击
28     // 如果点击完视图后还要把将要做的事情写进自身视图内部,无疑是一个高耦合状态
29     
30     switch (_type) {
31         case TouchViewTypeChangeColor:{
32             self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
33         }
34             break;
35         case TouchViewTypeChangeBounds:{
36             self.transform = CGAffineTransformScale(self.transform, 1, 2);// 纵向伸缩
37         }
38         default:
39             break;
40     }
41 
42     //-----------------Target - Action----------------------
43     // 我们完全可以利用 Target - Action:当视图被点击的时
44     // 这样视图不做任何处理,而是通知某对象执行某个方法
45     [_target performSelector:_action withObject:self afterDelay:0.1];
46 }
47 
48 @end
复制代码

// - ViewController.m

复制代码
 1 #import "ViewController.h"
 2 #import "TouchView.h"
 3 @implementation ViewController
 4 
 5 - (void)viewDidLoad {
 6     [super viewDidLoad];
 7 
 8     // --------------使用枚举----------------
 9     // 改变颜色
10     TouchView *touchView01 = [[TouchView alloc] initWithFrame:CGRectMake(50, 50, 100, 100) type:TouchViewTypeChangeColor];
11     touchView01.backgroundColor = [UIColor orangeColor];
12     [self.view addSubview:touchView01];
13 
14     // 改变尺寸
15     TouchView *touchView02 = [[TouchView alloc] initWithFrame:CGRectMake(220, 100, 50, 50) type:TouchViewTypeChangeBounds];
16     touchView02.backgroundColor = [UIColor purpleColor];
17     [self.view addSubview:touchView02];
18 
19     //---------------------- target - action:----------------------
20     // 方式一:直接通过成员变量赋值
21     TouchView *touchView03 = [[TouchView alloc] initWithTarget:self action:@selector(changeColor:)];
22     touchView03.frame = CGRectMake(50, 200, 100, 100);
23     touchView03.backgroundColor = [UIColor redColor];
24     [self.view addSubview: touchView03];
25 
26     TouchView *touchView04 = [[TouchView alloc] initWithTarget:self action:@selector(changeFrame:)];
27     touchView04.frame = CGRectMake(220, 250, 50, 50);
28     touchView04.backgroundColor = [UIColor orangeColor];
29     [self.view addSubview:touchView04];
30 
31     // 方式二:通过属性赋值
32     TouchView *touchView05 = [[TouchView alloc] initWithFrame:CGRectMake(50, 400, 50, 50)];
33     touchView05.target = self;
34     touchView05.action = @selector(changeFrame:);
35     touchView05.backgroundColor = [UIColor blackColor];
36     [self.view addSubview:touchView05];
37 
38 }
39 
40 // frame
41 -(void)changeFrame:(TouchView *)touchView{
42     touchView.frame = CGRectMake(arc4random()%220, arc4random()%486, 80, 80);
43 }
44 
45 // Color
46 - (void)changeColor:(TouchView *)touchView{
47     touchView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
48 }
49 
50 @end
复制代码

 

posted on   低头捡石頭  阅读(48)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示