Question

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)
 {}
}
Was it helpful?

Solution

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 :)

OTHER TIPS

You can use gesture recognizers:

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top