2014-09-24 56 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

相關問題