UI进阶第九发:iOS常用事件

1.IOS事件类型


1>触摸事件

2>加速计事件

3>远程控制器


2.响应者对象: UiResponder

1>含义与作用:

继承了UiResponder 的对象(UIApplication,UIView,UIewController)

只有继承了UiResponder的对象才可以接收并处理事件


2>UiResponder对象方法

one:触摸事件

开始触摸: - (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;

two:加速计事件

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

three:远程控制事件

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


3.UIView事件


是UIResponder的子类,使用UIview事件步骤:自定义View,继承UIView实现方法。


1>开始触摸view

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

2>在view上移动(随着手指的移动,会持续调用该方法)

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

3>手指离开view

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

4>触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法

(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

5> touches参数:保存的就是UITouch对象


4.UITouch对象


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

2>一根手指对应一个UITouch对象

3>UITouch的作用

one:保存着跟手指相关的信息,比如触摸的位置、时间、阶段

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

      (注意:在销毁前始终只创建一个对象,不断更新)

three:当手指离开屏幕时,系统会销毁相应的UITouch对象


注意:iPhone开发中,要避免使用双击事件

one:因为touchs参数只对应一个UITouch对象,一个手指对应一个UITouch

two:想要使用双击事件,将storyboard的属性勾上,允许双击事件的发生




5.UITouch属性


1>当前触摸时的窗口

@property(nonatomic,readonly,retain) UIWindow      *window;

2>当前触摸时的视图

@property(nonatomic,readonly,retain) UIView      *view;

3>短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击

@property(nonatomic,readonly) NSUInteger          tapCount;

4>记录了触摸事件产生或变化时的时间(单位:秒)

@property(nonatomic,readonly) NSTimeInterval      timestamp;

5>当前触摸事件所处的状态

@property(nonatomic,readonly) UITouchPhase        phase;


UITouchPhase是一个枚举类型,包含:

UITouchPhaseBegan(触摸开始)

UITouchPhaseMoved(接触点移动)

UITouchPhaseStationary(接触点无移动)

UITouchPhaseEnded(触摸结束)

UITouchPhaseCancelled(触摸取消)


6.UITouch方法


1>返回当前触摸在view上的位置

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

2>返回上一个触摸点的位置

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

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


7.UIEvent


1>每产生一个事件,就会产生一个UIEvent对象

2>UIEvent:称为事件对象,记录事件产生的时刻和类型

3>常见属性

one:事件类型: @property(nonatomic,readonly) UIEventType    type;

two:      @property(nonatomic,readonly) UIEventSubtype  subtype;

three.事件产生时间     @property(nonatomic,readonly) NSTimeInterval  timestamp;



8.事件中的touches和event参数


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


2>4个触摸事件处理方法中,都有NSSet *touches和UIEvent *event两个参数.

     一次完整的触摸过程中,只会产生一个事件对象,4个触摸方法都是同一个event参数.


3>如果两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法

    touches参数中装着2个UITouch对象


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


9.补充:

1>NSSset和NSArray集合区别: NSSset无序(取东西:[set anyObject]),NSArray有序

2>__func__: 判断当前方法在哪个类中调用

3>技巧:如果在storyboard中向类中拖线拖不了,那么可以先在类中把属性创建好,在类中向storyboard对应的控件拖线,(反着拖)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值