2012-12-24 30 views
22

我已經創建了一個UICollectionView,並希望所有單元格都像iPhone上跳板的編輯模式一樣搖動。我創建了我的搖動代碼,但不知道如何實現它。我有自定義單元格,所以我假設它在那裏,但不知道它如何實現。謝謝。UICollectionViewCell Shake

#define degreesToRadians(x) (M_PI * (x)/180.0) 
#define kAnimationRotateDeg 0.5 
#define kAnimationTranslateX 1.0 
#define kAnimationTranslateY 1.0 

//startJiggling 

int count = 1; 
CGAffineTransform leftWobble = CGAffineTransformMakeRotation(degreesToRadians(kAnimationRotateDeg * (count%2 ? +1 : -1))); 
CGAffineTransform rightWobble = CGAffineTransformMakeRotation(degreesToRadians(kAnimationRotateDeg * (count%2 ? -1 : +1))); 
CGAffineTransform moveTransform = CGAffineTransformTranslate(rightWobble, -kAnimationTranslateX, -kAnimationTranslateY); 
CGAffineTransform conCatTransform = CGAffineTransformConcat(rightWobble, moveTransform); 

    self.transform = leftWobble; // starting point 

    [UIView animateWithDuration:0.1 
          delay:(count * 0.08) 
         options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse 
        animations:^{ self.transform = conCatTransform; } 
        completion:nil]; 

回答

12

如果你把你在你的自定義單元格稱爲startJiggling方法的代碼,那麼你可以調用您的控制器此方法:

[self.collectionView.visibleCells makeObjectsPerformSelector:@selector(startJiggling)];

這保證了所有可見的單元格會開始搖擺。

然後,我還設置了一些標誌,指示您的細胞應該在collectionView:cellForItemAtIndexPath:中晃動並測試。如果是,請致電您的方法。

+0

除了所有偉大的,但最後一個單元格是晃動。當我打印出可見細胞計數並且數組數爲1時,可見細胞計數較低。有任何想法嗎? NSLog(@「visibleCellscount%d」,self.collectionView.visibleCells.count); 的NSLog(@ 「birdscount%d」,self.birds.count); – BDGapps

+0

對不起,沒有想法,沒有更多的代碼... – fguchelaar

+0

但所有細胞都可見?或者你必須滾動最後一個? – fguchelaar

1

廣場這行代碼在兩個地方:

[self.collectionView.visibleCells makeObjectsPerformSelector:@selector(startJiggling)];  
  1. -(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath

  2. -(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

2

如果任何人是如何做到這一點的雨燕2.0的好奇下面應該是一個很好的起點。

let animationRotateDegres: CGFloat = 0.5 
let animationTranslateX: CGFloat = 1.0 
let animationTranslateY: CGFloat = 1.0 
let count: Int = 1 

func wobble() { 
    let leftOrRight: CGFloat = (count % 2 == 0 ? 1 : -1) 
    let rightOrLeft: CGFloat = (count % 2 == 0 ? -1 : 1) 
    let leftWobble: CGAffineTransform = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegres * leftOrRight)) 
    let rightWobble: CGAffineTransform = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegres * rightOrLeft)) 
    let moveTransform: CGAffineTransform = CGAffineTransformTranslate(leftWobble, -animationTranslateX, -animationTranslateY) 
    let conCatTransform: CGAffineTransform = CGAffineTransformConcat(leftWobble, moveTransform) 

    transform = rightWobble // starting point 

    UIView.animateWithDuration(0.1, delay: 0.08, options: [.AllowUserInteraction, .Repeat, .Autoreverse], animations: {() -> Void in 
     self.transform = conCatTransform 
     }, completion: nil) 
} 

func degreesToRadians(x: CGFloat) -> CGFloat { 
    return CGFloat(M_PI) * x/180.0 
} 

當我想讓我的細胞搖晃我這樣做是這樣的:

for cell in collectionView.visibleCells() { 
     let customCell: MyCustomCell = cell as! MyCustomCell 
     customCell.wobble() 
    } 
+0

當我在contentView上應用轉換時,這起作用。所以:self.contentView.transform = conCatTransform。 – c0d3Junk13

+0

完美適合我! – garanda

0

更新到SWIFT 3語法:

let animationRotateDegres: CGFloat = 0.5 
let animationTranslateX: CGFloat = 1.0 
let animationTranslateY: CGFloat = 1.0 
let count: Int = 1 

func wobble() { 
    let leftOrRight: CGFloat = (count % 2 == 0 ? 1 : -1) 
    let rightOrLeft: CGFloat = (count % 2 == 0 ? -1 : 1) 
    let leftWobble: CGAffineTransform = CGAffineTransform(rotationAngle: degreesToRadians(x: animationRotateDegres * leftOrRight)) 
    let rightWobble: CGAffineTransform = CGAffineTransform(rotationAngle: degreesToRadians(x: animationRotateDegres * rightOrLeft)) 
    let moveTransform: CGAffineTransform = leftWobble.translatedBy(x: -animationTranslateX, y: -animationTranslateY) 
    let conCatTransform: CGAffineTransform = leftWobble.concatenating(moveTransform) 

    transform = rightWobble // starting point 

    UIView.animate(withDuration: 0.1, delay: 0.08, options: [.allowUserInteraction, .autoreverse], animations: {() -> Void in 
     self.transform = conCatTransform 
    }, completion: nil) 
} 

func degreesToRadians(x: CGFloat) -> CGFloat { 
    return CGFloat(M_PI) * x/180.0 
} 
+0

如何使用本????????????? @ user12345625 –

+0

EG。放在一個自定義UICollectionViewCell的代碼,並調用擺頻功能,當你需要的細胞撼動。 – user12345625

+0

我將它在awakeFromNib只是調用一次@ user12345625 –