9

Ich möchte eine Animation auf einem UICollectionViewCell starten, wenn der Benutzer auf eine Zelle klopft. Meine Idee war, die entsprechende Zelle in didSelectItemAtIndexPath auszuwählen und eine Animation auszulösen. Allerdings funktioniert das nicht:Animiere UICollectionViewCell auf Tap

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
// animate the cell user tapped on 
ProductCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductReuseID" forIndexPath:indexPath]; 

[UIView animateWithDuration:5.0 
         delay:0 
        options:(UIViewAnimationOptionAllowUserInteraction) 
       animations:^{ 
        NSLog(@"animation start"); 
        [cell.layer setBackgroundColor:[UIColor colorWithRed: 180.0/255.0 green: 238.0/255.0 blue:180.0/255.0 alpha: 1.0].CGColor]; 
       } 
       completion:^(BOOL finished){ 
        NSLog(@"animation end"); 
        [cell.layer setBackgroundColor:[UIColor whiteColor].CGColor]; 
       } 
]; 
} 

Eigentlich sind die Animation beginnt und gleichzeitig endet (obwohl animateWithDuration auf 5 gesetzt ist). Nächster Versuch war die Animation überspringen und einfach zum Beispiel eine andere Grenze Stil festgelegt: (wahrscheinlich, weil ich die Zelle manuell neu zu zeichnen haben)

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
// animate the cell user tapped on 
ProductCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductReuseID" forIndexPath:indexPath]; 

[cell.layer setBorderWidth:5.0f]; 
} 

Doch dies ändert nichts.

Haben Sie Ideen, wie Sie eine UICollectionViewCell animieren können, wenn der Benutzer darauf klickt?

Mit freundlichen Grüßen, Christian

Antwort

30

Es scheint, dass Sie die falsche Zelle sind zu erhalten. Das Senden der dequeueReusableCellWithReuseIdentifier:forIndexPath: Nachricht wird nicht die Zelle in der Ansicht in der IndexPath im zweiten Parameter verwendet, aber eine bereits verwendete, aber wiederverwendbare Zelle aus der Warteschlange entfernt; Wenn keine wiederverwendbare Zelle verfügbar ist, wird eine neue Zelle erstellt. Siehe Referenz 1 unten.

Ersetzen:

ProductCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductReuseID" forIndexPath:indexPath]; 

mit:

ProductCollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 

In Ihrem Code oben, sollten Sie die richtige Zelle geben, mit zu arbeiten.

Hier ist Ihr erstes Beispiel, neu geschrieben.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath(NSIndexPath *)indexPath 
{ 
    // animate the cell user tapped on 
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];  
    [UIView animateWithDuration:5.0 
      delay:0 
      options:(UIViewAnimationOptionAllowUserInteraction) 
       animations:^{ 
        NSLog(@"animation start"); 
        [cell setBackgroundColor:[UIColor colorWithRed: 180.0/255.0 green: 238.0/255.0 blue:180.0/255.0 alpha: 1.0]]; 
       } 
       completion:^(BOOL finished){ 
        NSLog(@"animation end"); 
        [cell setBackgroundColor:[UIColor whiteColor]]; 
       } 
    ]; 
} 

Referenzen:

+0

Vielen Dank! Das hat das Problem gelöst ... – itsame69

+0

OMG, danke dafür. – sabiland

2

Sie die Animation während wählen Sie anpassen können/tippen Sie auf die UICollectionViewCell mit Dauer benutzerdefinierte Animation durch den Code folgen. Sie müssen also nicht die Hintergrundfarbe ändern.

Mit folgenden Optionen - UIViewAnimationOption

  • UIViewAnimationOptionCurveEaseIn
  • UIViewAnimationOptionCurveEaseOut
  • UIViewAnimationOptionAllowUserInteraction

    UICollectionViewDelegate - didSelectItemAtIndexPath Methode

    UICollectionViewCell *uviCollectionCell = [collectionView cellForItemAtIndexPath:indexPath]; 
    
    [UIView animateWithDuration:0.4 delay:0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{ 
         NSLog(@"animation start"); 
         CALayer *layer = uviCollectionCell.layer; 
         CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity; 
         rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 15.0f * M_PI/180.0f, 1.0f, 0.0f, 0.0f); 
         layer.transform = rotationAndPerspectiveTransform; 
    } completion:^(BOOL finished) { 
         [UIView animateWithDuration:0.3 delay:0 options:(UIViewAnimationOptionCurveEaseOut) animations:^{ 
          NSLog(@"animation end"); 
          CALayer *layer = uviCollectionCell.layer; 
          CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity; 
          rotationAndPerspectiveTransform.m24 = 0; 
          rotationAndPerspectiveTransform =CATransform3DRotate(rotationAndPerspectiveTransform, 0.0f * M_PI/180.0f, 1.0f, 0.0f, 0.0f); 
          layer.transform = rotationAndPerspectiveTransform; 
         } completion:nil]; 
        } 
    ];