9

Kullanıcı bir hücreye dokunduğunda bir animasyona 'dan başlamak istiyorum. Fikrim, ilgili hücreyi didSelectItemAtIndexPath'da seçmek ve bir animasyonu tetiklemekti. Bununla birlikte, bu çalışmaz: (animateWithDuration 5'e ayarlanmış olsa da)Animate UICollectionViewCell on 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]; 
       } 
]; 
} 

Aslında, animasyon başlar ve aynı zamanda sona erer. Sonraki girişimi animasyon atlamak için ve sadece örneğin farklı bir sınır stilini ayarlayın: (? El hücreyi yeniden çizmek zorunda muhtemelen çünkü)

- (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]; 
} 

Ancak bu şeyi değiştirmez.

Kullanıcı, üzerine dokunduğunda UICollectionViewCell'i nasıl canlandıracağınıza dair herhangi bir fikriniz var mı?

Saygılarımızla Hıristiyan

cevap

30

Bu yanlış Cell elde ettiğimizi görünür. dequeueReusableCellWithReuseIdentifier:forIndexPath: iletisini göndermek değerini yapmaz, ikinci parametrede indexPath görünümünde kullanılan hücreyi elde eder, ancak daha önce kullanılmış ancak yeniden kullanılabilir bir hücreyi dequeues; Yeniden kullanılabilir bir hücre yoksa yeni bir tane oluşturulur. Aşağıdaki Referans 1'e bakınız.

Değiştirme:

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

olarak:

ProductCollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 

Kodunuzda yukarıda size çalışmak için uygun hücreyi vermelidir.

İşte yumruk örneğiniz, yeniden yazılmıştır.

- (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]]; 
       } 
    ]; 
} 

Referanslar:

+0

Çok teşekkürler! Bu sorunu çözdü ... Bunun için – itsame69

+0

OMG, thx. – sabiland

2

animasyonu özelleştirebilirsiniz seçme yaparken/kod izleyerek özel animasyon süresiyle UICollectionViewCell dokunun. Yani, arka plan rengini değiştirmenize gerek yok. Aşağıdaki seçenekleri ile

- UIViewAnimationOption

  • UIViewAnimationOptionCurveEaseIn
  • UIViewAnimationOptionCurveEaseOut
  • UIViewAnimationOptionAllowUserInteraction

    UICollectionViewDelegate - didSelectItemAtIndexPath yöntemi

    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]; 
        } 
    ]; 
    
İlgili konular