2014-09-24 65 views
0
- (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。如果我按下其他图像,那么该图像将再次出现。此时它正常工作。之后,我想拥有它,所以如果我按下空白处,然后图像视图隐藏。请给我一些想法。如何在UICollectionView中按下视图时关闭UIImageView

回答

0

添加到您的点触手势:

[recipeImageView removeFromSuperview]; 

但你的图像视图必须是任何函数访问。所以你可以创建一个函数,然后随时调用它。

UPDATE

对不起,延迟响应。在您的.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]; 
} 
+0

使用UIGestureRecognizerDelegate后,哪个委托方法将会调用 – 2014-09-24 06:15:03

+0

我编辑了我的答案,请检查。 – teach 2014-09-24 07:18:04

+0

好的,谢谢bro非常感谢。非常感谢 – 2014-09-24 08:31:07

0

保留一个指向该UIImageView的指针并在其上调用removeFromSuperview

0

在主视图上使用轻击手势可将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. 

} 
+0

不,我们不能这个bcoz按钮会隐藏所有的对象,所以滚动后也会停止。 – 2014-09-24 06:12:39

+0

然后最好的方法是使用轻击手势。然后识别您的手势功能中的轻拍视图。如果用户正在点击所需的视图,然后关闭可见的图像视图。我会在几分钟内用另一个代码更新我的答案。 – iHulk 2014-09-24 06:15:54

+0

我编辑了我的答案现在找到它。 – iHulk 2014-09-24 06:38:21

相关问题