我正在捕捉触摸事件。我需要区分两个事件 1) 用户触摸屏幕然后抬起手指 2) 用户触摸屏幕但不抬起手指 如何区分两个事件?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (isFirstCase)
{}
if (isSecondCase)
{}
}
您可以使用手势识别器:
首先,您需要注册手势识别器:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTap:)];
[myView addGestureRecognizer:tap];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[myView addGestureRecognizer:longPress];
然后你必须编写动作方法:
- (void)handleTap:(UITapGestureRecognizer *)gesture
{
// simple tap
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
// long tap
}
属性(NSSet *)touches
包含UITouch
对象,每个对象都包含几个有用的属性:
@property(nonatomic, readonly) NSUInteger tapCount
@property(nonatomic, readonly) NSTimeInterval timestamp
@property(nonatomic, readonly) UITouchPhase phase
@property(nonatomic,readonly,copy) NSArray *gestureRecognizers
typedef enum {
UITouchPhaseBegan,
UITouchPhaseMoved,
UITouchPhaseStationary,
UITouchPhaseEnded,
UITouchPhaseCancelled,
} UITouchPhase;
Phase 和 tapCount 是识别触摸类型的非常有用的属性。检查您是否可以使用 UIGestureRecognizers。NSArray *gestureRecognizers
- 与此特定触摸相关的对象数组。
祝你今天过得愉快 :)