How can I associate an IBAction event with an image?

I have an application that contains a moving image, and I want to animate the image using my custom animation.

How can I associate an IBAction event with an image?

How can I determine the coordinates on the screen where the user touched the image?

Please give your suggestions.

Thanks in advance, and your suggestions are most welcome.

+3
source share
2 answers

You can also do this:

first define the rectangle in which you will draw:

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

touchesEnded , , :

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

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

UIButtons . .


: UIGestureRecognizer 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];
+3

Source: https://habr.com/ru/post/1769308/


All Articles