事件处理(一)

1> 事件处理简介

  • 3大事件:(主要)触摸事件、加速计事件、远程控制事件。

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

  • 为什么继承UIResponder就能处理事件

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

2.加速计事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

3.远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
  • 想处理触摸事件,应该怎么办
  • UIView是UIResponder的子类,可以覆盖下列4个方法处理不同的触摸事件
1.一根或者多根手指开始触摸view,系统会自动调用view的下面方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

2.一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

3.一根或者多根手指离开view,系统会自动调用view的下面方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

4.触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
提示:touches中存放的都是UITouch对象
  • 重点UITouch

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

  • 一根手指对应一个UITouch对象

  • UITouch的作用
    保存着跟手指相关的信息,比如触摸的位置、时间、阶段

  • 当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指在的触摸位置

  • 当手指离开屏幕时,系统会销毁相应的UITouch对象
    提示:iPhone开发中,要避免使用双击事件!

1.触摸事件方法中的UITouch都是同一个对象,因为一根手指对应一个UITouch.当手指移动或者抬起,并不会产生一个新的UITouch对象给你,而是改变UITouch里面的属性,

1.默认三个方法里面只能获取到一个手指,为什么。
     UIView不支持多点触控
2.怎么才能有两个手指,两个手指同时按,并且视图支持多点触控

3.UITouch的tapCount有什么用?
    可以判断用户当前是双击还是单击
4.UITouch的phase有什么用? 
   根据这个属性,判断当前需要调用哪个处理事件方法,begin,move,end
  • 一般步骤

1.处理一个控件的触摸事件步骤
1.1 自定义View
1.2 重写touches方法

// NSSet:集合,无序
// 当手指触摸当前控件的时候调用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    
//    NSLog(@"%d",touches.count);
//    // 随机取一个
//    UITouch *touch = [touches anyObject];
//    // 获取当前点
//   CGPoint curP = [touch locationInView:self];
//     NSLog(@"%@",NSStringFromCGPoint(curP));
    NSLog(@"%s",__func__);
}

// 当手指在当前控件移动的时候调用
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
    // 获取UITouch
    UITouch *touch = [touches anyObject];
    
    // 获取当前点
    CGPoint curP = [touch locationInView:self];
    
    // 获取上一次点
    CGPoint preP = [touch previousLocationInView:self];
    
    // 获取x轴偏移量
    CGFloat offsetX = curP.x - preP.x;
    // 获取y轴偏移量
    CGFloat offsetY = curP.y - preP.y;
    
    // 修改当前控件的位置
    // 修改形变
    // CGAffineTransformMakeTranslation:相对于最开始的位置,使用Make,会把之前的所有形变清空,从零开始形变
    // 相对于上一次的形变
    
    self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
    
    NSLog(@"%s",__func__);
}

// 当手指抬起的时候调用
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s",__func__);
}

// 当触摸事件被打断的时候调用,比如打电话
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容