Following is code from my application.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch began");
[self performSelector:@selector(longTap) withObject:nil afterDelay:1.0];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch ended");
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(longTap) object:nil];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches moved");
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(longTap) object:nil];
}
-(void) longTap
{
NSLog(@"handle long tap..");
}
Hope this helps...
Yes! Thanks! I was looking for something just like this and it works perfectly!!
ReplyDeleteWelcome, I am glad that it helped...
ReplyDeleteWhy not use the UILongPressGestureRecognizer?
ReplyDeleteIt's a lot more elegant and straight forward...
UILongPressGestureRecognizer is introduced in new version of iPhone SDK. This API will not work on older version of iPhone SDK
ReplyDelete//Add gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView):
ReplyDelete// Add long tap for the main tiles
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
[tile addGestureRecognizer:longPressGesture];
[longPressGesture release];
-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(@"gestureRecognizer= %@",gestureRecognizer);
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
NSLog(@"longTap began");
}
}