我需要区分两个触摸事件。我如何区分两个事件?

时间:2012-03-27 08:16:53

标签: ios uievent

我正在抓住触摸事件。我需要区分两个事件 1)用户触摸屏幕然后抬起手指 2)用户触摸屏幕,不要抬起手指 我如何区分两个事件?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 if (isFirstCase)
 {}
 if (isSecondCase)
 {}
}

2 个答案:

答案 0 :(得分:2)

您可以使用手势识别器:

首先,您需要注册手势识别器:

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
}

答案 1 :(得分:2)

属性(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 - 与此特定触摸相关的此对象的数组。

度过愉快的一天:)