2012-12-24 5 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:にテストするべきであることを示すいくつかのフラグを設定します。 YESの場合は、メソッドを呼び出します。

+0

偉大です振とう。目に見える細胞数をプリントアウトし、アレイの数を1つカウントすると、目に見える細胞数は少なくなります。何か案は? NSLog(@ "visibleCellscount%d"、self.collectionView.visibleCells.count); NSLog(@ "birdscount%d"、self.birds.count); – BDGapps

+0

申し訳ありませんが、それ以上のコードはありません... – fguchelaar

+0

しかし、すべてのセルが表示されますか?または最後のものをスクロールする必要がありますか? – fguchelaar

1

場所2箇所のコード行:誰でもスウィフト2.0でこれを行う方法についての好奇心です

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

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

2

場合以下は良い出発点になるはずです。

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 
} 

私は私の細胞が私はこのようなそうぐらつくようにしたい:SWIFT 3構文に更新

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

これは、代わりにcontentViewでトランスフォームを適用したときに機能しました。したがって:self.contentView.transform = conCatTransform。 – c0d3Junk13

+0

私にとって完璧な作品です! – garanda

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 = 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

例:カスタムUICollectionViewCellにコードを置き、セルを振る必要があるときにウォッブル関数を呼び出します。 – user12345625

+0

awakeFromNibで設定しましたが、@ user12345625 –

関連する問題