在UICollectionView中按下视图时如何关闭UIImageView

时间:2014-09-24 05:42:16

标签: ios objective-c iphone

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewFlowLayout *myLayout=[[UICollectionViewFlowLayout alloc]init];
    myLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    [self.collectionView setCollectionViewLayout:myLayout animated:YES];

    UIImageView *recipeImageView=[[UIImageView alloc]initWithFrame:CGRectMake(35, 100, 250, 250)];
    recipeImageView.layer.borderColor = [UIColor redColor].CGColor;
    recipeImageView.layer.borderWidth = 8.0;
    [self.view addSubview:recipeImageView];
    NSString *selectedRecipeImageFileName = [self.getName objectAtIndex:indexPath.row];
    UIImage *selectedRecipeImage = [UIImage imageNamed:selectedRecipeImageFileName];
    recipeImageView.image = selectedRecipeImage;
}

我正在使用集合视图。首先,我按下任何图像,然后它将在视图上显示UIImageView。如果我按下另一张图像,则会再次显示该图像。此时它可以正常工作。在那之后,我想拥有它,如果我按下一个空白区域,那么隐藏图像视图。请给我一些想法。

3 个答案:

答案 0 :(得分:0)

保持指向该UIImageView的指针并在其上调用removeFromSuperview

答案 1 :(得分:0)

使用主视图上的点击手势从superView中删除UIImageView。

//inside viewDidLoad initilise tap gesture
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeImageView:)];
     tapRecognizer.numberOfTapsRequired = 1;

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewFlowLayout *myLayout=[[UICollectionViewFlowLayout alloc]init];
myLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
[self.collectionView setCollectionViewLayout:myLayout animated:YES];

UIImageView *recipeImageView=[[UIImageView alloc]initWithFrame:CGRectMake(35, 100, 250, 250)];
recipeImageView.layer.borderColor = [UIColor redColor].CGColor;
recipeImageView.layer.borderWidth = 8.0;

// tap gesture was initialised globally.
 [self.view addGestureRecognizer:tapGesture];

[self.view addSubview:recipeImageView];

NSString *selectedRecipeImageFileName = [self.getName objectAtIndex:indexPath.row];
UIImage *selectedRecipeImage = [UIImage imageNamed:selectedRecipeImageFileName];
recipeImageView.image = selectedRecipeImage;

}

现在在removeImageView:

- (void)removeImageView:(UITapGestureRecognizer*)sender {
     UIView *view = sender.view; 
     //Now here check the views on which user is taping and match if user is taping on desired view where you want remove the images from superView. and also remove the tap gesture from view.

}

答案 2 :(得分:0)

将此添加到您的点按手势:

[recipeImageView removeFromSuperview];

但您的图片视图必须可供任何功能使用。所以你可以创建一个函数,然后随时调用它。

<强>更新

很抱歉延迟回复。试试这个:

你的.h文件中的

首先在@interface ...添加<UIGestureRecognizerDelegate>

并在@interface之后:

@property UIImageView *recipeImageView;
您的.m文件中的

viewDidLoad

UIGestureRecognizer *taps = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeImageView)];
    taps.cancelsTouchesInView = NO;
    taps.delegate = self;
    [self.view addGestureRecognizer:taps];

以及.m文件中的某个地方:

-(void)removeImageView
{
    [_recipeImageView removeFromSuperview];
}