如何重新排列UIImages?

时间:2014-02-18 06:32:19

标签: ios iphone objective-c ios7

我正在创建一个应用程序,我在其中添加了一些UIImages作为UIview的子视图。现在,如果我删除图像,我想重新排列剩余的UIImages。

我怎样才能做到这一点?

修改

这是我到目前为止所尝试的:

for (int i=0; i<[array count]; i++) 
{ 
    id dict = [array objectAtIndex:i]; 
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)]; 
    [imageView setBackgroundColor:[UIColor clearColor]]; 
    [imageView setTag:i+1]; 
    [ImagesArray addObject:imageView]; 
    [self.view addSubView: imageView]; 
}

4 个答案:

答案 0 :(得分:0)

NSMutableArray中添加所有图片,并在 .h 文件中声明NSMutableArray

self.imagArray = [[NSMutableArray alloc] init];
[self.imagArray addObject:[UIImage imageWithName:@"image1.png"];
.
.
.
.
[self.imagArray addObject:[UIImage imageWithName:@"image_N.png"];

您可以通过self.imagArray轻松管理添加/删除图片。

如果要删除任何图像,请同时编写此代码

[self.imagArray removeObjectsAtIndexes:indexes]; 

答案 1 :(得分:0)

我假设您正在使用图像阵列。只需在删除索引后删除索引并登录即可刷新视图。

答案 2 :(得分:0)

首先将图像初始化为数组:

for (int i=0; i<[array count]; i++) 
{ 
    id dict = [array objectAtIndex:i]; 
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)]; 
    [imageView setBackgroundColor:[UIColor clearColor]]; 
    [ImagesArray addObject:imageView]; 
    [self.view addSubView: imageView]; 
}

删除图片:

NSUInteger deleteIdx = [ImagesArray indexOfObject:deleteImage];
[deleteImage removeFromSuperview];
[self relayoutViews:deleteIdx];

- (void)relayoutViews:(int)deleteIdx{
    for (int i=deleteIdx; i<[ImagesArray count]; i++) { 
        UIImageView *imageView = [ImagesArray objectAtIndex:i]; 
        imageView.frame = CGRectMake(20, 100*i+100, 60, 60)];
    }
}

答案 3 :(得分:0)

在标签的帮助下,您可以获得用户想要删除的图像视图。因此您必须在UIimageView上添加TapGesture,您必须使用绑定方法来删除图像。就像这样

for (int i=0; i<[array count]; i++) 

{ 
    id dict = [array objectAtIndex:i]; 
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)]; 
    [imageView setBackgroundColor:[UIColor clearColor]]; 
     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [imageView addGestureRecognizer:singleTap];
    [imageView setTag:i+1]; 
    [ImagesArray addObject:imageView]; 
    [self.view addSubView: imageView]; 
}


- (void)oneTap:(UIGestureRecognizer *)gesture {
    int myViewTag = gesture.view.tag;
    // Now with the help of tag you can remove object from you array and which you are want to remove and then you can reload you view 
}