Performing actions on specific image touch

Sometimes it may happen that you need to touch the images and on the touch of a specific image you need to perform some action.

So in todays post we shall discuss how to do this.

Design Phase: For this demo i have took two image views containing the images of spider man and super man and on the touch of each image i shall display a alertview.



Step 1: open Xcode and create a windows based application and add the UIViewController subclass file into it with an appropriate name, now create the view just like the above image of the design phase containing two instance of UIImageView class.

Step 2: Once you have completed the design now its time to add the instance method of the UIResponder class called as the touch began

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


Step 3: It's time to give body to that method, the entire code looks like this

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
pnt = [[touches anyObjectlocationInView:self.view];
if (CGRectContainsPoint(SpiderManImageView.framepnt)) 
{
UIAlertView *spiderManAlertView = [[UIAlertView alloc]initWithTitle:@"iPhone by radix" message:@"Spider man image touched" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
[spiderManAlertView show];
[spiderManAlertView release];
}
else if(CGRectContainsPoint(SupermanImageView.framepnt))
{
UIAlertView *superManAlertView = [[UIAlertView alloc]initWithTitle:@"iPhone by radix" message:@"Super man image touched" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
[superManAlertView show];
[superManAlertView release];
}
}


Code Explanation: The CGRectContainspoint is a function which accepts 2 parameter one is the rectangle who's points are to be examined and other is the parameter for the examiner point that will be checked and then just apply a simple if clause. The pnt is a variable of type CGPoint which contains the information about the point where the user has currently touched.

Step 4: Select the appDelegate.m file and this view to the iPhone window and then run the application.

Step 5: When you run the application you will get the final output.




I hope that this post was helpful to you, if you have any queries then kindly let me know until then happy iCoding and have a great Day.

Comments

  1. This is good sample, but user still need to run application.
    How can we make phone sound and vibrate when you touch on screen, which is showing a WAP site.
    For example, it show a site introducing picture of new car, if you touch the car key on the picture, it will sound line engine is starting and also vibrating.
    Tks

    ReplyDelete
  2. @Hai Binh: May be you may combine your business logic of this tutorial with my other tutorial given below

    http://iphonebyradix.blogspot.in/2011/07/vibrate-iphone-device.html

    Hope that helps

    Happy iCoing and have a great Day.

    ReplyDelete
  3. We can use UITapGestureRecognizer to detect touch:


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UITapGestureRecognizer *taplabelGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTap)];
           [SpiderManImageView  addGestureRecognizer:taplabelGesture];
        [taplabelGesture release];
    UITapGestureRecognizer *taplabelGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTap1)];
           [SupermanImageView  addGestureRecognizer:taplabelGesture1];
        [taplabelGesture1 release];
    }
    -(void) gestureTap
    {
       NSLog("Spider man image touched");
    }
    -(void) gestureTap1
    {
       NSLog("Super man image touched");
    }

    ReplyDelete

Post a Comment