触摸的target-action delegate设计思想的运用
来源:互联网 发布:linux获得root权限 编辑:程序博客网 时间:2024/06/15 08:46
目标:当点击一个类的视图对象的时候,做一些响应,点击该类不同的视图对象做出的响应不同
而触发时机是点击该视图时,所以在- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event这个方法让代理去执行任务
1.定义协议
@classTouchView;//当定义协议的时候TouchView还没出现
@protocol TouchViewDelegate <NSObject>
//协议中存在四个方法,对应触摸的四个时机,当视图对象接收到不同的触摸事件之后,通过协议中的方法通知代理对象
@optional
- (void)touchViewTouchesBegan:(TouchView *)touchView;
- (void)touchViewTouchesMoved:(TouchView *)touchView;
- (void)touchViewTouchesEnded:(TouchView *)touchView;
- (void)touchViewTouchesCancelled:(TouchView *)touchView;
@end
@interface TouchView :UIView
@property (nonatomic,assign) id<TouchViewDelegate>delegate;
//代理是通过属性的形似出现的
@end
2.遵守协议
@interfaceDelegateViewController ()<TouchViewDelegate>
@end
3.设置代理
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorcyanColor];
TouchView *redView = [[TouchViewalloc]initWithFrame:CGRectMake(20,50, 280, 40)];
redView.backgroundColor = [UIColorredColor];
redView.tag =100;
//为视图指定代理,来处理视图的触摸事件
redView.delegate =self;
[self.viewaddSubview:redView];
[redViewrelease];
TouchView *blueView = [[TouchViewalloc]initWithFrame:CGRectMake(20,120, 280, 40)];
blueView.backgroundColor = [UIColorblueColor];
blueView.tag =101;
blueView.delegate =self;
[self.viewaddSubview:blueView];
[blueViewrelease];
//grayView
TouchView *grayView = [[TouchViewalloc]initWithFrame:CGRectMake(20,450, 280, 100)];
grayView.backgroundColor = [UIColorgrayColor];
grayView.tag =102;
grayView.delegate =self;
[self.viewaddSubview:grayView];
[grayViewrelease];
}
4.让代理做事情的一个时机
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//原理:
//如果代理实现了协议的方法,就让代理调用方法,如果没有实现就不要调用
//判断代理是否实现了对应的方法(判断一个对象是否实现某个特定的方法)
if ([self.delegate respondsToSelector:@selector(touchViewTouchesBegan:)]) {
//触摸开始时通知代理做相应的操作
[self.delegate touchViewTouchesBegan:self];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//respondsToSelector:@selector 返回BOOL类型
if ([self.delegate respondsToSelector:@selector(touchViewTouchesMoved:)]) {
//触摸移动时通知代理做相应的操作
[self.delegate touchViewTouchesMoved:self];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([self.delegate respondsToSelector:@selector(touchViewTouchesEnded:)]) {
//触摸结束时通知代理做相应的操作
[self.delegate touchViewTouchesEnded:self];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([self.delegate respondsToSelector:@selector(touchViewTouchesCancelled:)]) {
//触摸取消时通知代理做相应的操作
[self.delegate touchViewTouchesCancelled:self];
}
}
5.代理做的事情- (void)touchViewTouchesBegan:(TouchView *)touchView
{
//匹配的用switch
switch (touchView.tag) {
case 100:
touchView.backgroundColor = [UIColorrandomColor];
break;
case 101:
touchView.superview.backgroundColor = [UIColor randomColor];
break;
case 102:
touchView.center =CGPointMake(arc4random() %200 + 100, arc4random() %400 + 100);
break;
default:
break;
}
}
- 触摸的target-action delegate设计思想的运用
- target - action设计模式的思想
- target...action和delegate的异同之处
- delegate / target, action / block
- UI阶段的 Target/Action设计模式
- target --action设计模式,以及delegate设计模式
- target --action设计模式,以及delegate设计模式
- MVC设计模式:Struts的Action思想
- iOS通讯模式(KVO、Notification、Delegate、Block、Target-Action的区别)
- iOS通讯模式(KVO、Notification、Delegate、Block、Target-Action的区别)
- iOS 对于Target-Action设计模式的理解
- Delegate,Action,Func的应用
- 调整思想的运用
- Android触摸事件的ACTION
- 常见的6种传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值 block传值 通知传值
- 导航控制器下UIViewController之间的传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值
- iOS-target/action模式,delegate模式
- Target-Action 设计模式
- Unique Binary Search Trees II 不同的树个数(重重重)
- POJ1580 水题,积累!
- SQL查询重复数据和清除重复数据
- 数据结构学习笔记(二)---求阶乘(递归与非递归)
- 网站性能测试PV到TPS的转换以及TPS的波动
- 触摸的target-action delegate设计思想的运用
- position属性absolute与relative 详解
- 基于ContentObserver来动态取消或添加屏幕超时任务
- wxpython 颜色对话框 ColourDialog
- 共线点Max Points on a Line
- C#中 MouseLeftButtonDown MouseLeftButtonUp MouseMove 使用
- 位排序
- 二天个人提供乳房和天赋规划图
- 最近需要看的几本书