2012-12-24 1 views
22

Ho creato un UICollectionView e vorrei che tutte le celle si muovessero come la modalità di modifica del trampolino su iPhone. Ho creato il mio codice shake ma non so come implementarlo. Ho celle personalizzate, quindi presumo che entri lì ma non so come verrà implementato. Grazie.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]; 

risposta

12

Se mettete sei codice in un metodo chiamato startJiggling nella tua cella personalizzato, allora si potrebbe chiamare questo metodo nel controller:

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

che assicura che tutte le celle visibili saranno inizia a tremare.

Quindi ho impostato anche un contrassegno che indica che le celle dovrebbero oscillare e testare per quello in collectionView:cellForItemAtIndexPath:. Se SÌ, chiama il tuo metodo.

+0

Ottimo, tranne tutti, ma l'ultima cella è agitazione. quando stampo il conteggio delle celle visibili e l'array ne conta il numero 1. il numero di celle visibili è inferiore. Qualche idea? NSLog (@ "visibleCellscount% d", self.collectionView.visibleCells.count); NSLog (@ "birdscount% d", self.birds.count); – BDGapps

+0

Scusa, non ne ho idea, senza più codice ... – fguchelaar

+0

Ma tutte le celle sono visibili? O devi scorrere per l'ultimo? – fguchelaar

1

Luogo questa riga di codice in due punti:

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

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

2

Se qualcuno è curioso su come fare questo in Swift 2.0 del di seguito dovrebbe essere un buon punto di partenza.

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 
} 

Quando voglio fare le mie cellule oscillazione Lo faccio in questo modo:

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

Questo ha funzionato quando ho applicato la trasformazione sul contentView invece. Quindi: self.contentView.transform = conCatTransform. – c0d3Junk13

+0

Funziona perfettamente per me !! – garanda

0

aggiornato alla rapida 3 sintassi:

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

Come usare questo ????????????? @ user12345625 –

+0

Es. inserisci il codice in un UICollectionViewCell personalizzato e chiama la funzione Wobble quando hai bisogno di scuotere la cella. – user12345625

+0

L'ho impostato su awakeFromNib ma ho chiamato una sola volta @ user12345625 –