How can I detect which UIImageView was selected with a touch in touchesBegan?
The user can create a unlimited number of UIImageViews with a button press with this code:
- (IBAction) addPicture:(id)sender {
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 45.0, 324, 52.0)];
imageView.tag = a;
imageView.image = [UIImage imageNamed:@"Picture.png"];
a = a + 1;
[self.view addSubview:imageView];
[imageView release];
}
So the first UIImageView gets the tag 1 and the second 2 and so on... Now how can I find out, which UIImageView was select by the user with a touch? I think, I have to do this in touchesBegan, but as I said, I don´t know how to get the right UIImageView.
For example in my app-idea the user can create images with a button and then he select a picture with a touch and can move it and resize it.
T开发者_JAVA百科hanks for your help.
Instead of using tag of UIImageView, why don't you create your own subclass of UIImageView?
You can overwrite touchesBegan method in the subclass, so you can detect a touch.
Then in the subclass, you can move or resize a picture which the subclass has.
The SDK is fairly flexible; there are actually quite a number of ways to go about this:
Subclass UIImageView, as tomute has already mentioned. You would then respond to your UIImageView's touches in each object's touchesBegan, touchesMoved & touchesEnded methods.
If your UIImageViews don't handle the touch events then they'll be passed to the super view where you could check in the superview by asking
if ([[touch view] isKindOfClass:[UIImageView class]]
then perhaps
switch ([[touch view] tag])
case (1) {
…
break;
}
case (2) {
…
break;
}
The event will continue getting passed 'up' until something in the chain handles the touch.
Review Event Handling and the responder chain.
精彩评论