iOS开发经验总结ios工具大全ios

事件的传递-底层

2015-09-28  本文已影响82人  HeavenWong

事件的产生和传递

  touchesBegan…
  touchesMoved…
  touchedEnded…


如何查找处理事件的最佳的view

// 点击白色view: 触摸事件 -> UIApplication -> [UIWindow hitTest] -> [whiteView hitTest] -> [ori hitTest] -> [green hitTest]

// 使用系统的做法, 相当于重写了, 但是没有覆盖系统的做法
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSLog(@"%s",__func__);
    return [super hitTest:point withEvent:event]; // 调用系统的做法
    return 0 ;
}

// 覆盖系统的hitTest方法, 让第0个子控件为最佳处理事件的view
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    return self.subviews[0];
}
// 除了根控制器可以处理事件, 其余的都不可以
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    return NO;
}
点击了绿色的view:
UIApplication -> UIWindow(keyWindow) -> 白色 -> 绿色
点击了蓝色的view:
UIApplication -> UIWindow -> 白色 -> 橙色 -> 蓝色
点击了黄色的view:
UIApplication -> UIWindow -> 白色 -> 橙色 -> 蓝色 -> 黄色

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



UIView的触摸事件处理

- (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
提示:touches中存放的都是UITouch对象

UITouch

UITouch的作用


UITouch的属性

@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;

UITouch的方法

// 返回值表示触摸在view上的当前的位置
- (CGPoint)locationInView:(UIView *)view;

// 这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))

调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置

// 该方法记录了前一个触摸点的位置
- (CGPoint)previousLocationInView:(UIView *)view;

UIEvent

@property(nonatomic,readonly) UIEventType    type;
@property(nonatomic,readonly) UIEventSubtype  subtype;
@property(nonatomic,readonly) NSTimeInterval  timestamp;
一次完整的触摸过程,会经历3个状态:
触摸开始:- (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

还要更新 + 完善!!

上一篇 下一篇

猜你喜欢

热点阅读