- Touch Tutorial - Part One (touchesEnded Event)
- Touch Tutorial - Part Two (touchesBegan Event)
- Touch Tutorial - Part Three (Zoom In/Out)
- TouchTutorial - Part Four (pan left/right)
In the second part of the touch tutorial series, I will show you how can you use the touchesBegan event. It is recommended that you read the first tutorial in this series. You can download the source code for the first tutorial here.
This is the method which we will implement in ImgViewController.m file.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
}
In this method we will display an alert if the user has touched the image for 2 seconds. This how the source code will look like
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
switch ([allTouches count]) {
case 1: { //Single touch
//Get the first touch.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
switch ([touch tapCount])
{
case 1: //Single Tap.
{
//Start a timer for 2 seconds.
timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:@selector(showAlertView:) userInfo:nil repeats:NO];
[timer retain];
} break;
case 2: //Double tap.
break;
}
} break;
case 2: { //Double Touch
} break;
default:
break;
}
}
NSSet *allTouches = [event allTouches];
switch ([allTouches count]) {
case 1: { //Single touch
//Get the first touch.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
switch ([touch tapCount])
{
case 1: //Single Tap.
{
//Start a timer for 2 seconds.
timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:@selector(showAlertView:) userInfo:nil repeats:NO];
[timer retain];
} break;
case 2: //Double tap.
break;
}
} break;
case 2: { //Double Touch
} break;
default:
break;
}
}
We get all the touches and for now we will only look at the code for single touch. Get the touch object at index 0 (since it is a single touch, there will be an object at this index) and find out the tap count. If the tap count is one then create a new timer for 2 seconds and give a method which will be called, when the timer is elapsed. The timer variable is declared in the header file of ImgViewController.h file. "showAlertView" is the method which is called when the user presses the screen for 2 seconds.
To display an alert view, we need to implement the UIAlertViewDelegate. This is how the header file will be changed
#import
@interface ImgViewController : UIViewController {
IBOutlet UIImageView *imgView;
NSTimer *timer;
}
@end
@interface ImgViewController : UIViewController {
IBOutlet UIImageView *imgView;
NSTimer *timer;
}
@end
this is how "showAlertView" method will look like
-(void) showAlertView:(NSTimer *)theTimer {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Birth of a star" message:@"Timer ended. Event Fired."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Birth of a star" message:@"Timer ended. Event Fired."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
We do need to cancel the timer if any other method like touchesMoved, touchesEnded or touchesCanceled is called. We do this by first checking if the timer is active or not, if it is active then we call the "invalidate" method. This is to be done at the beginning of all the three methods mentioned.
Run this application in the simulator and see it working. The same technique can be used to display an alert sheet or any custom action that you want to perform.
You can download the source code from here and please leave me your comments.
Không có nhận xét nào:
Đăng nhận xét