14

मेरे पास UICollectionView में मूल ग्रिड है। यह UICollectionViewDelegateFlowLayout का उपयोग करके एक साधारण 2 कॉलम, एकाधिक पंक्ति लेआउट है। जब कोई सेल चुना जाता है, तो मैं पृष्ठभूमि को मंद करना चाहता हूं, सेल को स्क्रीन के केंद्र में फ़्लोट करना चाहता हूं और फिर चयनित सेल के आधार पर वर्कफ़्लो रखना चाहता हूं। मैं UICollectionViews पर बिल्कुल नया हूं, और मुझे इस बारे में जाने का सबसे अच्छा तरीका नहीं है।चयन पर UICollectionView सेल को एनिमेट करें

क्या मेरे पास कक्ष चुनने के लिए UICollectionView का कस्टम लेआउट होना चाहिए?

या वहाँ एक रास्ता मैं एक नए लेआउट

अगर कोई बस मुझे सही दिशा में चल रहा प्राप्त कर सकते हैं, मुझे लगता है कि मैं अनुसंधान करने के लिए निष्पादित करने के लिए कितना अच्छा हो जाएगा बनाए बिना चयनित सेल चेतन कर सकते हैं यह।

उत्तर

12

कई (और बल्कि हैकी) समाधानों को आजमाने के बाद, मैंने इसे अपना UICollectionView लेआउट लिखकर हल किया। पिछला समाधान मैं का प्रयास किया:

  • बनाने एक नया बिना UICollectionView और नकली का प्रयास ज़्यादा होना एक कस्टम UIView जोड़े मूल कोशिका यह डालने, पृष्ठभूमि मंद, और नए UIView
  • एनिमेट से आगे बढ़ सेल एनिमेट लेआउट

मैं इसे से बचने के लिए कोशिश कर रहा था, लेकिन उसके बाद मैं इसे कोडिंग में मिल गया है, यह एक बहुत कम दर्दनाक की तुलना में मैं मूल रूप से सोचा था।

ज:

@interface FMStackedLayout : UICollectionViewLayout 

@property(nonatomic,assign) CGPoint  center; 
@property(nonatomic,assign) NSInteger cellCount; 

-(id)initWithSelectedCellIndexPath:(NSIndexPath *)indexPath; 

@end 

मीटर:

#define ITEM_WIDTH 128.0f 
#define ITEM_HEIGHT 180.0f 

static NSUInteger  const RotationCount   = 32; 
static NSUInteger  const RotationStride  = 3; 
static NSUInteger  const PhotoCellBaseZIndex = 100; 

@interface FMStackedLayout() 

@property(strong,nonatomic) NSArray  *rotations; 
@property(strong,nonatomic) NSIndexPath *selectedIndexPath; 

@end 

@implementation FMStackedLayout 

#pragma mark - Lifecycle 
-(id)initWithSelectedCellIndexPath:(NSIndexPath *)indexPath{ 
    self = [super init]; 
    if (self) { 
     self.selectedIndexPath = indexPath; 
     [self setup]; 
    } 
    return self; 
} 

-(id)initWithCoder:(NSCoder *)aDecoder{ 
    self = [super init]; 
    if (self){ 
     [self setup]; 
    } 
    return self; 
} 

-(void)setup{ 
    NSMutableArray *rotations = [NSMutableArray arrayWithCapacity:RotationCount]; 
    CGFloat percentage = 0.0f; 

    for (NSUInteger i = 0; i < RotationCount; i++) { 
     // Ensure that each angle is different enough to be seen 
     CGFloat newPercentage = 0.0f; 
     do { 
      newPercentage = ((CGFloat)(arc4random() % 220) - 110) * 0.0001f; 
     } while (fabsf(percentage - newPercentage) < 0.006); 

     percentage = newPercentage; 

     CGFloat angle = 2 * M_PI * (1.0f + percentage); 
     CATransform3D transform = CATransform3DMakeRotation(angle, 0.0f, 0.0f, 1.0f); 
     [rotations addObject:[NSValue valueWithCATransform3D:transform]]; 
    } 

    self.rotations = rotations; 
} 

#pragma mark - Layout 
-(void)prepareLayout{ 
    [super prepareLayout]; 

    CGSize size = self.collectionView.frame.size; 
    self.cellCount = [self.collectionView numberOfItemsInSection:0]; 
    self.center = CGPointMake(size.width/2.0, size.height/2.0); 
} 

-(CGSize)collectionViewContentSize{ 
    return self.collectionView.frame.size; 
} 

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; 

    attributes.size = CGSizeMake(ITEM_WIDTH, ITEM_HEIGHT); 
    attributes.center = self.center; 
    if (indexPath.item == self.selectedIndexPath.item) { 
     attributes.zIndex = 100; 
    }else{ 
     attributes.transform3D = [self transformForPersonViewAtIndex:indexPath]; 
    } 

    return attributes; 
} 

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{ 
    NSMutableArray *attributes = [NSMutableArray array]; 
    for (NSInteger i = 0; i < self.cellCount; i++) { 
     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i 
                inSection:0]; 
     [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]]; 
    } 
    return attributes; 
} 

#pragma mark - Private 
-(CATransform3D)transformForPersonViewAtIndex:(NSIndexPath *)indexPath{ 
    NSInteger offset = (indexPath.section * RotationStride + indexPath.item); 
    return [self.rotations[offset % RotationCount] CATransform3DValue]; 
} 

@end 

फिर, अपने UICollectionView पर, आप

फोन

यहाँ रुचि किसी के लिए, मेरे कोड है

MyLayout *stackedLayout = [[MyLayout alloc] initWithSelectedCellIndexPath:indexPath]; 
[stackedLayout invalidateLayout]; 
[self.collectionView setCollectionViewLayout:stackedLayout 
            animated:YES]; 

सेल के बीच एनिमेशन कस्टम लेआउट द्वारा संभाला जाएगा।

+3

यह एक उत्तर नहीं है। ऐसा लगता है कि आपके पास जवाब है लेकिन साझा करने में रुचि नहीं है। – Fiveagle

+1

@ मार्क स्ट्रुज़िंस्की क्या आप अपना समाधान साझा कर सकते हैं? –

+0

यह कोड का एक छोटा सा हिस्सा है, लेकिन मैं किसी विशेष कारण के लिए वापस नहीं रोक रहा था। मैंने इसे किसी भी रुचि के लिए ऊपर पोस्ट कर दिया है। निश्चित नहीं है कि यहां डाउनवॉट क्यों थे। अंतिम समाधान ने एक विधि का उपयोग किया जो कि एप्पल दस्तावेज़ों में पूरी तरह से प्रलेखित है: http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewLayout_class/Reference/Reference.html। मैं इसे टालने की कोशिश कर रहा था क्योंकि यह काफी काम है। –

6

एक बहु-भाग प्रश्न होने लगता है, इसलिए मैं इसका हिस्सा जवाब दूंगा।

  1. UICollectionviewCellsस्वचालित रूप से उजागर या चयन पर समायोजित नहीं है। यह केवल आपको बता देंगे जब सेल पर प्रकाश डाला या चयन किया जाता है एक अपवाद के साथ:

अधिकांश भाग के लिए, संग्रह दृश्य संकेत मिलता है कि यह चयनित या हाइलाइट किया गया है केवल एक सेल के गुणों को संशोधित करता है; यह एक अपवाद के साथ, आपकी कोशिकाओं की दृश्य उपस्थिति को परिवर्तित नहीं करता है। यदि सेल की selectedBackgroundView संपत्ति में एक वैध दृश्य है, तो संग्रह दृश्य दिखाता है कि सेल को हाइलाइट किया गया या चयनित है या नहीं।

अन्यथा, आप दृश्य मैन्युअल पर प्रकाश डाला करना चाहिए।

collectionView:didDeselectItemAtIndexPath: 
collectionView:didHighlightItemAtIndexPath: 
collectionView:didSelectItemAtIndexPath: 
collectionView:didUnhighlightItemAtIndexPath: 
collectionView:shouldDeselectItemAtIndexPath: 
collectionView:shouldHighlightItemAtIndexPath: 
collectionView:shouldSelectItemAtIndexPath: 

: आमतौर पर, पूरे सेल के दोनों .alpha संपत्ति का समायोजन या बाहर गमागमन सेल अपने आप में एक पृष्ठभूमि UIImageView की .image संपत्ति एक या निम्न विधियों, जो <UICollectionViewDelegate> प्रोटोकॉल में उपलब्ध हैं का अधिक उपयोग करके आपके बाकी प्रश्न के लिए, मेरी इच्छा है कि मैं अधिक सहायता प्राप्त कर सकूं, लेकिन मैं कस्टम व्यू एनीमेशन पर धाराप्रवाह नहीं हूं।

+0

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

संबंधित मुद्दे