10、iOS 开发之事件处理

一、只有继承了UIResponder的对象才能接受并处理事件–”响应者事件”

1.触摸事件

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

覆盖以上4种方法来处理不同的触摸事件

2.UITouch对象
触摸屏幕时会产生一个与手指相关的UITouch对象

1> 属性

@property(nonatomic,readonly,retain) UIWindow    *window;       // 所在窗口
@property(nonatomic,readonly,retain) UIView      *view;         // 所在视图
@property(nonatomic,readonly) NSUInteger          tapCount;     // 点击屏幕的次数
@property(nonatomic,readonly) NSTimeInterval      timestamp;    // 事件的时间
@property(nonatomic,readonly) UITouchPhase        phase;        // 事件所处的状态

2> 方法

- (CGPoint)locationInView:(UIView *)view;
- (CGPoint)previousLocationInView:(UIView *)view;

3.UIEvent
每产生一个事件,就会产生一个UIEvent对象

1> 属性

@property(nonatomic,readonly) UIEventType     type;
@property(nonatomic,readonly) UIEventSubtype  subtype;
@property(nonatomic,readonly) NSTimeInterval  timestamp;

4.事件的产生和传递

1> 将触摸事件加入到由UIApplication管理的事件队列中
2> UIApplication将最前面的事件发给主窗口keyWindow
3> 主窗口在"视图的层次结构中"找到一个合适的子控件来处理触摸事件
4> "合适的子控件"中调用touchs的方法做具体事件处理

响应者链条的事件传递过程

* 如果View的控制器存在,就传递给控制器;如果控制器不存在,则将其传递给父视图
* 在视图层次结构的最顶级视图,如果也不能处理收到的事件消息,则将事件或消息传递给window对象处理
* 如果window对象也不能处理,则将事件或消息传递给UIApplication 对象
* 如果UIApplication也不能处理该事件,则将其丢弃

父控件不能接受触摸事件,子控件就不能接受触摸事件

5.UIView不接收触摸事件的三种情况

userInteractionEnabled = NO;
hidden = YES;
alpha = 0.0~0.01
// 提示:UIImageView的userInteractionEnabled默认是NO,因此UIImageView以及它的子控件是不能接受触摸事件的

二、手势识别器UIGestureRecognizer

UIGestureRecognizer是一个抽象类,定义了所有的手势的基本行为,只有使用子类才能处理具体的手势

UITapGestureRecognizer //(敲击)
UIPinchGestureRecognizer//(捏合,用于缩放)
UIPanGestureRecognizer//(拖拽)
UISwipeGestureRecognizer//(轻扫)
UIRotationGestureRecognizer//(旋转)
UILongPressGestureRecognizer//(长按)

1.手势识别器的用法都差不多
1> 创建手势识别器对象

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapIconView:wa)]

2> 设置手势识别器对象的具体属性

// 连续敲击2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起敲击
tap.numberOfTouchesRequired = 2;

3> 添加手势识别器到对应的view上

[self.iconView addGestureRecognizer:tap];

4> 监听手势的触发

[tap addTarget:self action:@selector(tapIconView:)];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值