Nitish Lamba
Nitish Lamba

Reputation: 157

How to give action on click of an image

I am developing an app in which what i want is when a user clicks on an image in an image view

An Action should be fired(like when a button is clicked).

I don't want button image. I just want an image view and action on it.

Kindly help.

Upvotes: 0

Views: 319

Answers (2)

Maulik
Maulik

Reputation: 19418

According to your comment, if you want to know about image's index then you can do this by tag property of image view. Assign your image view's tag property as per your need and get the tag on Touch event.

For example :

Add UITapGestureRecognizer

ITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                action:@selector(tap:)];
tap.numberOfTapsRequired = 1;

[self addGestureRecognizer:tap];

[tap release];

Use its delegate method : - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event

// called when touch is began or when user touches 
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject]; 

    int indexOfImageView = touch.view.tag;
}

Hope it helps you...

Upvotes: 0

Mani
Mani

Reputation: 1841

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
    singleTap.numberOfTapsRequired = 1;
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:singleTap];
    [singleTap release];


-(void)tapDetected{
    NSLog(@"single Tap on imageview");

}

Upvotes: 4

Related Questions