2012-12-24 18 views
22

Ich habe eine UICollectionView erstellt und möchte, dass alle Zellen wie der Bearbeitungsmodus des Sprungbretts auf dem iPhone schütteln. Ich habe meinen Shake-Code erstellt, weiß aber nicht, wie ich ihn umsetzen soll. Ich habe benutzerdefinierte Zellen, also nehme ich an, dass es dort hineingeht, aber ich weiß nicht, wie es implementiert wird. Danke.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]; 

Antwort

12

Wenn Sie Sie in einer Methode startJiggling sind Code setzen in Ihrer benutzerdefinierten Zelle genannt wird, dann könnte man diese Methode in Ihrem Controller nennen:

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

Das stellt sicher, dass alle sichtbaren Zellen anfangen zu wackeln.

Dann würde ich auch ein Flag setzen, das angibt, dass Ihre Zellen wackeln und in collectionView:cellForItemAtIndexPath: dafür testen sollten. Wenn JA, rufen Sie Ihre Methode auf.

+0

Great except alle, aber die letzte Zelle ist Schütteln. Wenn ich die Anzahl der sichtbaren Zellen ausdrücke und das Array um 1 abzählt, ist die Anzahl der sichtbaren Zellen geringer. Irgendwelche Ideen? NSLog (@ "visibleCellscount% d", self.collectionView.visibleCells.count); NSLog (@ "birdscount% d", self.birds.count); – BDGapps

+0

Sorry, keine Ahnung dafür, ohne mehr Code ... – fguchelaar

+0

Aber alle Zellen sind sichtbar? Oder musst du für den letzten scrollen? – fguchelaar

1

Dieses Codezeile an zwei Stellen:

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

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

2

Wenn jemand neugierig, wie dieser 2.0 in Swift tun die Unten sollte ein guter Ausgangspunkt sein.

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 
} 

Wenn ich möchte, dass meine Zellen machen wackeln ich so so tun:

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

Das funktionierte, als ich die Transformation stattdessen auf die contentView anwendete. Also: self.contentView.transform = conCatTransform. – c0d3Junk13

+0

Funktioniert perfekt für mich !! – garanda

0

Aktualisiert swift 3 Syntax:

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

Wie man das benutzt ????????????? @ user12345625 –

+0

Eg. Setzen Sie den Code in eine benutzerdefinierte UICollectionViewCell und rufen Sie die Wobble-Funktion auf, wenn Sie die Zelle zum Schütteln benötigen. – user12345625

+0

Ich setze es in watchFromNib, aber nur einmal anrufen @ user12345625 –