views:

211

answers:

1

I need to know which method is used to identify tap/mouse click.I know
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}

which are triggerd when cursor moved. But I want to do following things-

I have an image. When i will click it, similar two image will be created.And so on

Can anyone help????Advanced thanx for your reply.

A: 

The tap events are handled not by the touches method callbacks in UIView, but as targets-actions in UIControl. UIControl is a subclass of UIView, and adds abstractions for taps, drags and other common user actions, so that you do not need to implement the logic yourself.

To add a action for the user tapping a control simply do this:

[myControl addTarget:self
              action:@selector(didSelectFoo:) 
           forEvents:UIControlEventTouchUpInside];

This is usable both if you subclass UIControl yourself, or if you use any of the provided controls such as UIButton, UITextField, UILabel, etc.

PeyloW
This isn't entirely correct. A UIView subclass can indeed respond to touch events by overriding –touchesBegan:withEvent:, –touchesMoved:withEvent:, –touchesEnded:withEvent:, and -touchesCancelled:withEvent: , or a controller object can manage those touch events through delegate methods with the same interfaces.
Brad Larson
Brad I think you missed _"so that you do not need to implement the logic yourself"_. Yes you can override `touchedBegan:withEvent:` and the rest on a `UIControl` subclass as well. But the whole idea is that for 99 out of 100 use cases you should not need to.
PeyloW
The phrase "The tap events are handled not by the touches method callbacks in UIView, but as targets-actions in UIControl" seems to say that UIViews can't respond to touch events, when they can through subclassing or through delegation. With delegation, no code even needs to be added to UIView. It's just a matter of wording that might be confusing to a new iPhone developer.
Brad Larson