Search Posts in my Blog

Thursday, 1 March 2012

Handling Touches and Events in iPhone



In this post we will learn how to deal with touches and events in the iPhone.
The iPhone can detect human touches and with the help of this feature of the iPhone we can enhance our application to deal with human touches and for that the iPhone SDK has a class called as the UIResponder class.

UIResponder class: The UIResponder class defines an instance of objects that respond to and handle events.
There are two general kind of events:

       1) Touches events which are handled by the following functions:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

The parameters of these methods associate touches with their events—”especially touches that are new or have changed—”and thus allow responder objects to track and handle the touches.

 Any time a finger touches the screen, is dragged on the screen, or lifts from the screen, aUIEvent object is generated. The event object contains UITouch objects for all fingers on the screen or just lifted from it.

2) iOS 3.0 introduced system capabilities for generating motion events, specifically the motion of shaking the device. The event-handling methods for these kinds of events are

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

In this post we will have a look at touches part where I will try to display an alert view when the user will swipe left and right, so here’s a view at our final output




Step 1: Open Xcode and create a windows based application and add a UIViewController subclass file to it with the name myviewController, now you must have two files with the name myviewController.h and myviewController.m .

Step 2: Now open the documentation and in the search bar type UIResponder and copy paste some of the methods which are given below

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event


Since we are dealing with touches we require the above methods it is not compulsory that you will be using all the 4 methods that handle the touch event, it is totally depended upon your business logic , so for this application I will just use the above function of the UIResponder class.

Step 3: Here’s the logic for this application, I will have an initialPoint (which would be the variable of CGPoint declared in the myviewController.h file) which will keep a record of the initial point from where the user began his touch and then I will have a variable called endPoint (variable of CGPoint declared in myviewController.h) which will keep a record about the endPoint of the user i.e the point where the users finger where lifted off from the screen and then I will subtract these two points and then will manipulate the final result. Here’s a look at the code

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
initialPoint = [[touches anyObject]locationInView:self.view];
}


In the touch began code I have took the initialPoint from where the touch has began and saved it in the variable initialPoint 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

endPoint = [[touches anyObject]locationInView:self.view];
if ((endPoint.x - initialPoint.x) > 100.f) {
UIAlertView *obj = [[UIAlertView alloc]initWithTitle:@"Touches and Events"message:@"Swipe right" delegate:nil cancelButtonTitle:@"cancel"otherButtonTitles:@"ok",nil];
[obj show];
[obj release];
}
else if((endPoint.x - initialPoint.x) < -100.f) {
UIAlertView *obj1 = [[UIAlertView alloc]initWithTitle:@"Touches and Events"message:@"Swipe left" delegate:nil cancelButtonTitle:@"cancel"otherButtonTitles:@"ok",nil];
[obj1 show];
[obj1 release];

}
}




In the touch end function I have stored the final point from where the users fingers were off from the screen in the  endPoint variable and then in the if loop I am performing some calculation to determine when to show the alert view.

[obj release]: this line will be explained in my upcoming post it is related with memory management

Step 4: Now go to the appDelegate.m file and add this view into the iPhone simulator like this

#import "TouchesandEventsAppDelegate.h"
#import "myviewController.h"

@implementation TouchesandEventsAppDelegate
@synthesize window;
#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
myviewController *obj = [[myviewController alloc]init];
[window addSubview:obj.view];
        [window makeKeyAndVisible];
    return YES;
}


Step 5: Now here’s a view at our final output






i Hope this post has helped you

No comments:

Post a Comment