9

पर UICollectionViewCell को एनिमेट करें, जब उपयोगकर्ता किसी सेल पर टैप करता है तो UICollectionViewCell पर कुछ एनीमेशन शुरू करना चाहता हूं। मेरा विचार didSelectItemAtIndexPath में संबंधित सेल का चयन करना था और एक एनीमेशन ट्रिगर करना था। हालांकि, यह काम नहीं करता:टैप

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

वास्तव में, एनीमेशन शुरू होता है और एक ही समय में समाप्त हो जाती है (हालांकि animateWithDuration 5 के लिए सेट है)। अगला प्रयास एनीमेशन छोड़ था और केवल उदाहरण के लिए एक अलग बॉर्डर शैली सेट: (? शायद इसलिए क्योंकि मैं मैन्युअल रूप से सेल पुनः बनाने का है)

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

बहरहाल, यह कुछ भी नहीं बदलता है।

क्या आपके पास कोई विचार है कि उपयोगकर्ता ने उस पर टैप किए जाने पर UICollectionViewCell को एनिमेट कैसे किया?

सधन्यवाद, ईसाई

उत्तर

30

ऐसा लगता है कि आप गलत सेल प्राप्त कर रहे हैं। dequeueReusableCellWithReuseIdentifier:forIndexPath: संदेश भेजना दूसरे पैरामीटर में इंडेक्सपैथ पर दृश्य में उपयोग में सेल प्राप्त करता है, लेकिन पहले इस्तेमाल किए गए लेकिन पुन: प्रयोज्य सेल को हटा देता है; यदि कोई पुन: प्रयोज्य सेल उपलब्ध नहीं है, तो एक नया बनाया गया है। नीचे संदर्भ 1 देखें।

की जगह:

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

के साथ:

ProductCollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 

अपने कोड में ऊपर, आप के साथ काम करने के लिए उचित सेल देना चाहिए।

यहां आपका मुट्ठी उदाहरण है, फिर से लिखा गया है।

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

संदर्भ:

+0

बहुत बहुत धन्यवाद! इसने समस्या हल की ... – itsame69

+0

ओएमजी, इसके लिए thx। – sabiland

2

आप एनीमेशन अनुकूलित कर सकते हैं, जबकि चयन/कोड का पालन करते हुए कस्टम एनिमेशन अवधि के साथ UICollectionViewCell टैप करें। तो, आपको इसके पृष्ठभूमि रंग को बदलने की जरूरत नहीं है।

निम्नलिखित विकल्पों के साथ

- UIViewAnimationOption

  • UIViewAnimationOptionCurveEaseIn
  • UIViewAnimationOptionCurveEaseOut
  • UIViewAnimationOptionAllowUserInteraction

    UICollectionViewDelegate - didSelectItemAtIndexPath विधि

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