2014-01-31 58 views
10

我正在使用UICollectionView,我正在顯示手機照片庫中的所有圖像。UICollectionView中的圖像在向下滾動時自動翻轉?

當我點擊任何圖像時,圖像會翻轉並顯示一些關於圖像的信息。

當用戶再次點擊同一圖像時,圖像會再次翻轉並顯示原始圖像。

問題是,每當我向下滾動UICollectionView時,最後一次選擇的圖像會自動翻轉並顯示有關圖像的信息。

如何解決此問題。

下面是一些代碼:

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UICollectionViewCell *cell1 = [collectionView cellForItemAtIndexPath:indexPath]; 
     if(old_path!=NULL){ 
     UICollectionViewCell *cell2 = [collectionView cellForItemAtIndexPath:old_path]; 
     [UIView transitionFromView:cell2.selectedBackgroundView toView:cell2.contentView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil]; 

    } 
if(old_path==indexPath&&flag) 
{ 
    [cell1 setSelected:NO]; 
    [UIView transitionFromView:cell1.selectedBackgroundView toView:cell1.contentView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil]; 
    flag=FALSE; 
} 
else{ 
    [UIView transitionFromView:cell1.contentView toView:cell1.selectedBackgroundView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil]; 
    flag=TRUE; 
} 
old_path=indexPath; 
} 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 
    ALAsset *asset = assets[indexPath.row]; 
    NSLog(@"Description : %@",[asset description]); 
    UIImage *img=[self imageWithImage:[UIImage imageWithCGImage:[asset thumbnail]] convertToSize:CGSizeMake(150, 150)]; 
    UIView *contents = [[UIView alloc]initWithFrame:cell.bounds]; 
    contents.backgroundColor = [UIColor colorWithPatternImage:img]; 

    [cell.contentView addSubview:contents]; 

    UIView *backgroundView = [[UIView alloc]initWithFrame:cell.bounds]; 
    backgroundView.backgroundColor = [UIColor yellowColor]; 
    UIButton *del=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    del.frame= CGRectMake(backgroundView.frame.origin.x+20,  backgroundView.frame.origin.y+20, 100, 40); 
    [del setTitle:@"Delete" forState:UIControlStateNormal]; 
    [del addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside]; 
    [backgroundView addSubview:del]; 
    UIButton *cancel=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    cancel.frame= CGRectMake(backgroundView.frame.origin.x+20, backgroundView.frame.origin.y+80, 100, 45); 
    [cancel setTitle:@"Cancel" forState:UIControlStateNormal]; 
    [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside]; 
    [backgroundView addSubview:cancel]; 

    cell.selectedBackgroundView = backgroundView; 
    [cell bringSubviewToFront:cell.selectedBackgroundView]; 
    return cell; 
} 

這裏,old_path包含最後選擇的圖像的索引。

+1

需要進行一些代碼。我們不知道你正在做的究竟是什麼。 – Albara

+1

爲* cellForItemAtIndexPath:*方法添加代碼,以便我們瞭解如何繪製單元格。 –

回答

1

的主要問題是可能與UIView的轉換方法:

[UIView transitionFromView:cell1.selectedBackgroundView toView:cell1.contentView duration:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil]; 

UICollectionViewCellcontentViewselectedBackgroundView不應該這樣用搞砸,因爲小區管理它們的佈局。此代碼將完全刪除內容視圖並將其替換爲背景視圖,後者視圖在選中時應位於內容視圖後面,並且在未選中時從視圖層次結構中移除。

正確的方式來完成你在做什麼(顯示/隱藏圖像響應一個水龍頭)將執行圖像視圖本身和內容視圖上的另一個子視圖之間的過渡。


也有可能存在的東西在你調整大小代碼多數民衆贊成翻轉圖像,但很難不爲imageWithImage:convertToSize:代碼說。或許,這將是更有效地擺脫那種方法,做這樣的事情:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds]; 
imageView.contentsMode = UIViewContentModeScaleAspectFill; 
imageView.clipsToBounds = YES; 
imageView.image = [UIImage imageWithCGImage:[asset thumbnail]]; 
[cell.contentsView addSubview:imageView]; 

一對夫婦的其他意見:

Collection視圖細胞重用,這意味着您的實施collectionView:cellForItemAtIndexPath:可能會最終將一大堆圖像視圖添加到已出隊多次的單元。更好的解決方案是子類UICollectionViewCell並在其init方法中添加自定義視圖。

代碼old_path==indexPath實際上並不測試兩個索引路徑之間的相等性,只是如果兩個變量在內存中具有相同的地址。改爲使用[oldPath isEqual:indexPath]

0

如果不明確進行子類化和處理,則無法在背景視圖上進行自定義動畫。因爲selectedBackgroundViewbackgroundView不同,收集視圖會自動將selectedBackgroundView置頂。添加Apple文檔以供參考。

// The background view is a subview behind all other views. 
// If selectedBackgroundView is different than backgroundView, it will be placed above the background view and animated in on selection. 

https://developer.apple.com/library/iOs/documentation/UIKit/Reference/UICollectionViewCell_class/Reference/Reference.html