views:

31

answers:

2

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.

+1  A: 

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];
fluchtpunkt
we have a bouncing ball application where on the user touch we want to change the direction of the ball and the ball is an image. So i m confused that if i use a button as you said then how will i get it to move as the ball moves.
PARTH
the exact same way you would move the UIImageView. Both are UIViews so there should be no difference. But to be honest, for moving objects I would not use the button approach. That just doesn't feel good. Please see my edit int the answer.
fluchtpunkt
thanks anyways.
PARTH
A: 

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
        }
}
JonLOo
touchRect=CGRectMake(screen.width/2-screen.width/8, 0, screen.width/4, screen.height/5); What is this frame about? i am not very clear about that. please do explain as i m still a newbee.
PARTH
how to associate touchesEnded event with imageview in IB? Thanks in advance
PARTH
Sorry, its just an example GRectMake(x position, y position, x size, y size) just create a Rect. Its like creating an invisible square in any position you want so you can check if is there something inside it in your screen
JonLOo
its not neccesary to add an IBAction here, if you implement touchesEnded whenever you touch your screen it will be called
JonLOo
in your case you can make a rect with your bouncing ball every time you touch the screen and check if you are touching wherever the ball is.
JonLOo
you are welcome :)
JonLOo
What is CCDirector?. I am getting the error: "CCDirector undeclared"
PARTH
sorry that is from cocos2d you have to use : -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //Get all the touches.NSSet *allTouches = [event allTouches];//Number of touches on the screen//Get the first touch.UITouch *touch = [[allTouches allObjects] objectAtIndex:0]; CGPoint targetPoint = [[touches anyObject] locationInView:self];} and work around that
JonLOo
Ya thanks a lot. :)
PARTH
i have edited the answer, maybe its more clear that way
JonLOo
Im happy if I have helped you :)
JonLOo
In "locationInView:self" i m getting an error stating : Incompatible Objective-C types "struct <MyViewName> *" expected "struct UIView *" What needs to be done?
PARTH