Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 标识UIImageView的标记_Objective C_Xcode - Fatal编程技术网

Objective c 标识UIImageView的标记

Objective c 标识UIImageView的标记,objective-c,xcode,Objective C,Xcode,在我的应用程序中,用户可以通过编程创建多个UIImageView,并将标记成功分配给所有ImageView。然后,我用下面的方法检测(在这种情况下通过触摸移动)图像的标签,并根据标签改变颜色 int touchedtag = touch.view.tag; NSUInteger tagCount = touchedtag; switch (tagCount) { case 1: image1.backgroundColor = [UIC

在我的应用程序中,用户可以通过编程创建多个UIImageView,并将标记成功分配给所有ImageView。然后,我用下面的方法检测(在这种情况下通过触摸移动)图像的标签,并根据标签改变颜色

int touchedtag = touch.view.tag;

    NSUInteger tagCount = touchedtag;
    switch (tagCount) {
        case 1: 
            image1.backgroundColor = [UIColor redColor];
            break;
        case 2: 
            image1.backgroundColor = [UIColor yellowColor];
            break;
        case 3: 
            image1.backgroundColor = [UIColor brownColor];
            break;
        case 4: 
            image1.backgroundColor = [UIColor blackColor];

            break;
        default :

            break;

    }

如你所见,共有4例。但是,如果用户创建了50个UIImageView,我是否需要创建50个案例,或者是否可以用更少的代码完成此识别过程??(考虑到颜色的改变是为了看它是否有效,但这将仅用于识别标签)

如果只使用一种颜色,则会更容易,但如果您想要50种不同的颜色,则需要创建50个案例。

对于50种不同的
UIImageView
标签,您还可以编程定义您的标签

for(int i = 0 ;i < 50;i++)
{
     UIImageView *yourIMG = [[UIImageView alloc] init];
     yourIMG.tag = i;
}
尝试使用
for
循环可编程调整这些参数,例如为每个图像增加红色2个单位或减少蓝色10个单位


希望它能帮助你

您肯定不想仅仅为了检查一个数值而创建50个案例,或者根本不想创建任何案例。您将如何处理标记值?指定任意颜色

无论哪种方式,这里都是示例代码的复制,而不使用case语句

NSArray *colors = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor yellowColor], [UIColor brownColor], [UIColor blackColor], nil];

NSUInteger touchedtag = touch.view.tag;
image1.backgroundColor = [colors objectAtIndex:touchedtag];
当然,如果标记大于数组中的颜色数,则会出现问题。您可以使用其他逻辑解决这个问题,或者通过将第二行更改为:

NSUInteger touchedtag = touch.view.tag % colors.count;

您已经有了触摸产生的imageview,那么您还想识别什么?你到底想达到什么目的?那么你已经成功了,因为你有这个观点。如果选择的意思是给它一个边框或阴影或其他东西,那么只需在视图对象上这样做。您甚至可以将视图存储到currentSelection变量或类似的东西中。因此,要查看我是否理解,您只需要查看所选UIImage的标记,对吗?如果是这样,为什么不尝试使用NSLog(@“tag:%d”,tagCount)?
NSUInteger touchedtag = touch.view.tag % colors.count;