Parth Bhatt
Parth Bhatt

Reputation: 19469

How can i associate an IBAction event with an image?

I have a application which contains a moving image and i want to animate the image using a my custom animation.

How can i associate an IBAction event with the image?

How can i detect the coordinates on screen where the user touched the image?

Please Give Your Suggestions.

Thanks in advance and Your Suggestions are Most Welcome.

Upvotes: 3

Views: 3290

Answers (2)

JonLOo
JonLOo

Reputation: 4939

You can also do this way:

first define the rect where you image will be:

touchRect=CGRectMake(screen.width/2-screen.width/8, 0, screen.width/4, screen.height/5);

then with touchesEnded method you can define what you want to do when you touch over that zone:

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

        CGPoint targetPoint = [[touches anyObject] locationInView:self];
        if (CGRectContainsPoint(touchRect,  targetPoint)){
            //YOUR CODE HERE
        }
}

Upvotes: 1

Matthias Bauch
Matthias Bauch

Reputation: 90117

You could use custom UIButtons with your image. All the touch handling code is already builtin.


Edit: UIGestureRecognizer also work on UIImageViews:

UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]] autorelease];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[self.view addSubview:imageView];

Upvotes: 3

Related Questions