1. UITouch对象概念:
当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象,可以说一根手指对应一个UITouch对象。
2. UITouch的作用:
a.保存着跟本次手指触摸相关的信息,比如触摸的位置、时间、阶段;
b.当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指所在的触摸位置;
c.当手指离开屏幕时,系统会销毁相应的UITouch对象;
提示:iPhone开发中,要避免使用双击事件!
3. UITouch的6个属性
@interfaceUITouch : NSObject
//手指触摸屏幕的时间戳
@property(nonatomic,readonly)NSTimeInterval timestamp;
//手指触摸的那个view所在的window
@property(nonatomic,readonly,retain)UIWindow *window;
//手指触摸的那个view
@property(nonatomic,readonly,retain)UIView *view;
//短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击
@property(nonatomic,readonly)NSUInteger tapCount;
//记录了触摸事件产生或变化时的时间,单位是秒
@property(nonatomic,readonly)NSTimeInterval timestamp;
//当前触摸事件所处的状态
@property(nonatomic,readonly)UITouchPhase phase;
4. 两个对象方法:
-(CGPoint)locationInView:(UIView *)view;
返回值表示触摸在view上的位置
这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))
调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置
-(CGPoint)previousLocationInView:(UIView*)view;
该方法记录了前一个触摸点的位置
5. 补充:
typedefNS_ENUM(NSInteger, UITouchPhase) {
// whenever a finger touches the surface.
UITouchPhaseBegan,
// whenever a finger moves on the surface.
UITouchPhaseMoved,
// whenever a finger is touching the surface but hasn't movedsince the previous event.
UITouchPhaseStationary,
// whenever a finger leaves the surface.
UITouchPhaseEnded,
// whenever a touch doesn't end but we need to stop tracking (e.g.putting device to face)
UITouchPhaseCancelled,
};
本文详细介绍了iOS开发中UITouch对象的概念及其作用,并列举了UITouch的六个重要属性和两个对象方法,同时解释了UITouchPhase状态的不同含义。
2034

被折叠的 条评论
为什么被折叠?



