Objective c 触摸与UIImageView数组关联

Objective c 触摸与UIImageView数组关联,objective-c,ios,uiimageview,touchesbegan,Objective C,Ios,Uiimageview,Touchesbegan,我有一个带有UIImageView的数组。假设当前阵列中有5个UIImageView。如何知道当前正在触摸哪个UIImageView 谢谢 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; if ([touch view] == ??) { } } 最简单的解决方案之一是设置视图的标记属性 @prop

我有一个带有UIImageView的数组。假设当前阵列中有5个UIImageView。如何知道当前正在触摸哪个UIImageView

谢谢

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];

    if ([touch view] == ??)
    {

    }
}

最简单的解决方案之一是设置视图的
标记
属性

@property(nonatomic) NSInteger tag
通过这种方式,您可以轻松检索被触摸的对象:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    if ([[touch view] tag] == 1)
    {
        // ...
    }
}


您可以在Interface Builder中或直接在代码中设置此属性。

正如另一张海报所说,使用标记。不过,他的代码有点乱。我从来不会像他那样打电话给酒店。你宁愿这样做

[touch view].tag
通过这种方式,其他人将更容易阅读您的代码。此外,您还可以设置UIImageView的标记,如下所示:

yourImageView.tag = 1

其中yourImageView是图像。

为循环中的每个图像指定标记值

for(int i=0;i<5;i++)
{
view.tag=i
}

for(int i=0;i如何知道用户是否正在触摸任何UIImageView?类似于
if([touch view]==myArray)
?@RockBaby-不,此测试不会告诉您有用的信息。我会测试类似于:
[myArray containsObject:[touch view]]
。谢谢。这就是我现在正在做的。我在想,有没有没有没有循环的方法你可以使用NSpredicate,没有循环很简单