iOS-控件响应用户控制事件之事件处理

时间:2015-07-12 00:14:19   收藏:0   阅读:270

事件处理

响应者对象

UIResponder内部提供了以下方法来处理事件

//加速计事件

//远程控制事件

事件的参数

UITouch

UITouch的作用
UITouch的方法
- (CGPoint)locationInView:(UIView *)view;
- (CGPoint)previousLocationInView:(UIView *)view;

UIEvent

常见属性

事件类型
@property(nonatomic,readonly) UIEventType     type;
@property(nonatomic,readonly) UIEventSubtype  subtype;

事件产生的时间

@property(nonatomic,readonly) NSTimeInterval  timestamp;

事件的产生和传递

touchesBegan…
touchesMoved…
touchedEnded…

如果父控件不能接收触摸事件,那么子控件就不可能接收到触摸事件(掌握)

如何找到最合适的控件来处理事件?

原理

// point:是方法调用者坐标系上的触摸点的位置
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    // 1.判断下能否接收触摸事件
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.0) return nil;

    // 2.判断下点在不在控件上
    if ([self pointInside:point withEvent:event] == NO) return nil;

    // 3.从后往前遍历子控件
    int count = (int)self.subviews.count;

    for (int i = count - 1; i >= 0 ; i--) {
        // 取出显示在最前面的子控件
        UIView *childView =  self.subviews[i];

        // 转换成子控件坐标系上点
        CGPoint childP = [self convertPoint:point toView:childView];

        UIView *fitView = [childView hitTest:childP withEvent:event];

        if (fitView) {
            return fitView;
        }

    }

    // 表示没有比自己更合适的view
    return self;

}

事件传递的完整过程

如何判断上一个响应者

响应者链的事件传递过程

响应者链条示意图

技术分享

监听触摸事件的做法

UIGestureRecognizer

UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
UITapGestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)

原文:http://www.cnblogs.com/ShaoYinling/p/4639643.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!