How can i associate an IBAction event with an image?
I have a application which contains a moving image a开发者_JAVA百科nd 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.
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];
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
}
}
精彩评论