简体   繁体   English

如何在touchesBegan中通过触摸检测选择了哪个UIImageView?

[英]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: 用户可以通过按下以下代码来创建无限数量的UIImageViews:

- (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? 因此,第一个UIImageView获取标签1,第二个UIImage获取标签,依此类推...现在我如何找出用户触摸一下选择了哪个UIImageView? I think, I have to do this in touchesBegan, but as I said, I don´t know how to get the right UIImageView. 我认为,我必须在touchesBegan中执行此操作,但是正如我所说,我不知道如何获取正确的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. 例如,在我的应用程序想法中,用户可以使用按钮创建图像,然后通过触摸选择图片,然后可以移动图片并调整其大小。

Thanks for your help. 谢谢你的帮助。

Instead of using tag of UIImageView, why don't you create your own subclass of UIImageView? 为什么不使用UIImageView的标记,而不创建自己的UIImageView的子类?
You can overwrite touchesBegan method in the subclass, so you can detect a touch. 您可以覆盖子类中的touchesBegan方法,以便检测触摸。
Then in the subclass, you can move or resize a picture which the subclass has. 然后,在子类中,可以移动或调整子类具有的图片的大小。

The SDK is fairly flexible; 该SDK非常灵活; there are actually quite a number of ways to go about this: 实际上,有很多方法可以解决此问题:

Subclass UIImageView, as tomute has already mentioned. 子类的UIImageView,因为tomute已经提到。 You would then respond to your UIImageView's touches in each object's touchesBegan, touchesMoved & touchesEnded methods. 然后,您将在每个对象的touchesBegan,touchesMoved和touchesEnded方法中响应UIImageView的触摸。

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 如果您的UIImageViews不处理触摸事件,那么它们将被传递到超级视图,您可以通过询问来检查超级视图

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. 查看事件处理和响应者链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM