iOS Developer

iOS中的触摸事件

2017-01-04  本文已影响0人  小白文_Vincent
文艺求关注.png

<h5>iOS中的事件</h5>

在用户使用app过程中,会产生各种各样的事件
iOS中的事件可以分为3大类型


iOS中的事件.png

<h5>响应者对象</h5>

在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象”
lUIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件

<h5>UIResponder</h5>

lUIResponder内部提供了以下方法来处理事件
触摸事件

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

加速计事件

-(void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent*)event;
-(void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent*)event;
-(void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent*)event;

远程控制事件

- (void)remoteControlReceivedWithEvent:(UIEvent*)event;

<h5>UIView的触摸事件处理</h5>

UIView是UIResponder的子类,可以覆盖下列4个方法处理不同的触摸事件

-(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对象

<h5>UITouch</h5>

当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象
一根手指对应一个UITouch对象
UITouch的作用

当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指在的触摸位置
当手指离开屏幕时,系统会销毁相应的UITouch对象

提示:iPhone开发中,要避免使用双击事件!

<h5>UITouch的属性</h5>

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

<h5>UITouch的方法</h5>

-(CGPoint)locationInView:(UIView*)view;
// 返回值表示触摸在view上的位置
// 这里返回的位置是针对view的坐标系的(以view的左上角为原点(0,0))
// 调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置
- (CGPoint)previousLocationInView:(UIView*)view;
// 该方法记录了前一个触摸点的位置

<h5>UIEvent</h5>

每产生一个事件,就会产生一个UIEvent对象
UIEvent:称为事件对象,记录事件产生的时刻和类型
<b>常见属性—事件类型</b>

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

<b>常见属性—事件产生的时间</b>

@property(nonatomic,readonly)NSTimeInterval  timestamp;

UIEvent还提供了相应的方法可以获得在某个view上面的触摸对象(UITouch)

<h5>touches和event参数</h5>

一次完整的触摸过程,会经历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

4个触摸事件处理方法中,都有NSSet *touchesUIEvent *event两个参数
<b>一次完整的触摸过程中,只会产生一个事件对象,4个触摸方法都是同一个event参数</b>

如果两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法,touches参数中装着2个UITouch对象

如果这两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,并且每次调用时的touches参数中只包含一个UITouch对象

根据touches中UITouch的个数可以判断出是单点触摸还是多点触摸

<h5>事件的产生和传递</h5>

<b>* 注意:如果父控件不能接收触摸事件,那么子控件就不可能接收到触摸事件</b>

<h5>UIView不接收触摸事件的三种情况</h5>

提示:UIImageView的userInteractionEnabled默认就是NO,因此UIImageView以及它的子控件默认是不能接收触摸事件的

<h5>触摸事件处理的详细过程</h5>

用户点击屏幕后产生的一个触摸事件,经过一系列的传递过程后,会找到最合适的视图控件来处理这个事件

找到最合适的视图控件后,就会调用控件的touches方法来作具体的事件处理

touchesBegan方法的默认做法是将事件顺着响应者链条向上传递,将事件交给上一个响应者进行处理

<h5>响应者链条</h5>

<h5>事件传递的完整过程</h5>

<b>如何判断上一个响应者</b>

<h5>响应者链的事件传递过程</h5>

<h5>监听触摸事件的做法</h5>

如果想监听一个view上面的触摸事件,之前的做法是:

iOS3.2之后,苹果推出了手势识别功能(GestureRecognizer),在触摸事件处理方面,大大简化了开发者的开发难度

<h5>UIGestureRecognizer</h5>

敲击事件

// 点按手势
- (void)setupTap {

    // 1.创建手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    
    tap.delegate = self;  // 遵守协议 <UIGestureRecognizerDelegate>
    
    // 2.添加手势
    [self.imageV addGestureRecognizer:tap];
    
}
// 3.实现手势方法
- (void)tapGesture {

    NSLog(@"%s", __func__);
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    // 让当前的图片,左边不能点击,右边接收点击
    CGPoint currentPoint = [touch locationInView:self.imageV];
    if (currentPoint.x > self.imageV.frame.size.width * 0.5) {
        
        // 在右边
        return YES;
    }else {
    
        // 在左边
        return NO;
    }
}

长按手势

// 长按手势
- (void)setupLongPressGesture {

    // 1.创建手势
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
    
    // 2.添加手势
    [self.imageV addGestureRecognizer:longPressGesture];
}

// 3.长按时调用(当长按移动时,该方法会持续调用)
- (void)longPressGesture: (UILongPressGestureRecognizer *)longPressGesture {

    NSLog(@"%s", __func__);
    
    // 判断手势的状态
    if (longPressGesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始长按");
    }else if (longPressGesture.state == UIGestureRecognizerStateChanged) {
    
        NSLog(@"长按时移动");
    }else if (longPressGesture.state == UIGestureRecognizerStateEnded) {
    
        NSLog(@"手指离开");
    }
}

轻扫手势

// 轻扫手势
- (void)setupSwipeGesture {

    // 1.创建手势
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    
    // 设置轻扫的方向(一个轻扫手势只能对应一个方向)
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    
    // 2.添加手势
    [self.imageV addGestureRecognizer:swipeGesture];
}

- (void)swipeGesture:(UISwipeGestureRecognizer *)swipeGesture {

    
    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
        
        NSLog(@"left");
    }else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {
    
        NSLog(@"right");
    }
    
    NSLog(@"%s", __func__);
}

拖动手势


// 拖动手势
- (void)setupPanGesture {

    // 1.创建手势
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    
    // 2.添加手势
    [self.imageV addGestureRecognizer:panGesture];
}

// 当拖动View时调用
- (void)panGesture:(UIPanGestureRecognizer *)panGesture {

    // 获取偏移量(相对于最原始的偏移量)
    CGPoint translationPoint = [panGesture translationInView:self.imageV];
    NSLog(@"%@", NSStringFromCGPoint(translationPoint));
    
    self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, translationPoint.x, translationPoint.y);
    
    // 让它相对于上一次而不是原始偏移量
    [panGesture setTranslation:CGPointZero inView:self.imageV];
}

捏合手势

// 捏合手势
- (void)setupPinchGesture {

    // 1.创建手势
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
    
    // 2.添加手势
    [self.imageV addGestureRecognizer:pinchGesture];
}

// 当缩放时调用
- (void)pinchGesture:(UIPinchGestureRecognizer *)pinchGesture {

    // 针对于原始尺寸的缩放
    self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pinchGesture.scale, pinchGesture.scale);
    
    // 复位
    [pinchGesture setScale:1];
}

旋转手势

// 旋转手势
- (void)setupRotationGesture {

    // 1.创建手势
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
    
    // 2.添加手势
    [self.imageV addGestureRecognizer:rotationGesture];
}

- (void)rotationGesture:(UIRotationGestureRecognizer *)rotationGesture {

    self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotationGesture.rotation);
    
    // 复位
    [rotationGesture setRotation:0];
}

<b>* 注意:默认情况下,不支持多个手势一起使用,需通过代理方法来实现</b>

    ... ...
    // 设置代理方法
    panGesture.delegate = self;
    pinchGesture.delegate = self;
    rotationGesture.delegate = self;
    
    // 遵守代理协议;<UIGestureRecognizerDelegate>

// 实现代理方法
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;
}
关注一下又不会怀孕.png
上一篇 下一篇

猜你喜欢

热点阅读