I need to differentiate two touch events. How can I differentiate two events?
Вопрос
I'm catching touch events. I need to differentiate two events 1) User touched a screen and then lifted a finger 2) User touched screen and don't lifted a finger How can I differentiate two events?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (isFirstCase)
{}
if (isSecondCase)
{}
}
Решение
Attribute (NSSet *)touches
contains UITouch
objects each of them contain several helpful properties:
@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 and tapCount is very usable property to identify the type of touch.
Check if you can use UIGestureRecognizers. NSArray *gestureRecognizers
- the array of this objects which related to this particular touch.
Have a nice day :)
Другие советы
You can use gesture recognizers:
- UITapGestureRecognizer for simple taps
- UILongPressGestureRecognizer for long taps
First you need to register the gesture recognizers:
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];
Then you have to write the action methods:
- (void)handleTap:(UITapGestureRecognizer *)gesture
{
// simple tap
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
// long tap
}
Не связан с StackOverflow