2012-09-26 8 views
10

नए UICollectionView में मुझे नहीं लगता कि UICollectionViewCell में छाया कैसे जोड़ें। मैं इसे किस तरह लूं। क्या मैं एक और दृश्य जोड़ूंगा?UICollectionViewCell छाया रंग

[self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowPath = [UIBezierPath bezierPathWithRect:rect].CGPath; 
    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowColor = [UIColor yellowColor].CGColor; 
    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowRadius = .5; 
    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowOpacity = .1; 

enter image description here

+2

यह बहुत अक्षम कॉल करने के लिए नहीं है '[self.collectionView cellForItemAtIndexPath: [self.collectionView indexPathForItemAtPoint: [पहचानकर्ता स्थानInView: [स्वयं दृश्य]]]] 'कई बार? –

उत्तर

1

सबसे अधिक संभावना है आपकी समस्या को सबसे अच्छा आपके हालात के लिए विशिष्ट होना How do I draw a shadow under a UIView?

को मौजूदा उत्तर के हल किया जाता है, तो आप शायद कोड है कि क्या निम्नलिखित कोड करता है क्या करना होगा होता (जिस सेल में आप रुचि रखते हैं उसे इंगित करने के लिए आपको अपना संग्रह कहां और कुछ इंडेक्सपैथ प्राप्त होता है):

UICollectionViewCell* collectionViewCell 
     = [collectionView dequeueReusableCellWithReuseIdentifier:DEFINED_IDENTIFIER forIndexPath:someIndexPath]; 
    collectionViewCell.layer.shadowPath = [UIBezierPath bezierPathWithRect:collectionViewCell.bounds].CGPath; 

सेल प्राप्त करने के स्पष्ट रूप से अन्य तरीके हैं। shadowPath सेट करने के लिए, महत्वपूर्ण बात दूसरी पंक्ति है।

+0

यह पूरे सेल को प्रभावित करने वाले तत्वों के चारों ओर एक छाया नहीं बना रहा है। कृपया ऊपर देखें। – BDGapps

+0

हमम ... अच्छी तरह से आपके मूल प्रश्न ने कहा "* नए UICollectionView में मुझे नहीं लगता कि UICollectionViewCell में छाया कैसे जोड़नी है। *"। यदि आप सेल में तत्वों के चारों ओर एक छाया रखना चाहते हैं, तो आपको UICollectionViewCell के सबव्यूज़ एकत्र करना होगा और फिर प्रत्येक पर छाया की सेटिंग को अलग-अलग करना होगा। –

0

आप परत पर छाया ऑफसेट संपत्ति सेट नहीं कर रहे हैं।

myCell.layer.shadowOffset = CGSizeMake(10,10); 
+0

यह अभी भी सफेद दृश्य पर छाया नहीं बना रहा है, यह इसे पूरे सेल पर सेट करता है। यह एक वास्तविक छाया सिर्फ एक रूपांतरित पृष्ठभूमि नहीं है। – BDGapps

2
[self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.masksToBounds = NO; 
42

आप NO को UIView पर masksToBounds स्थापित करने के लिए भूल रहे हैं। यह काम करना चाहिए:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath]; 

    cell.layer.masksToBounds = NO; 
    cell.layer.borderColor = [UIColor whiteColor].CGColor; 
    cell.layer.borderWidth = 7.0f; 
    cell.layer.contentsScale = [UIScreen mainScreen].scale; 
    cell.layer.shadowOpacity = 0.75f; 
    cell.layer.shadowRadius = 5.0f; 
    cell.layer.shadowOffset = CGSizeZero; 
    cell.layer.shadowPath = [UIBezierPath bezierPathWithRect:cell.bounds].CGPath; 
    cell.layer.shouldRasterize = YES; 

    return cell; 
} 
+0

क्या यह धीमा नहीं होगा? कोई विचार अगर हम इसे सेल सबक्लास में कर सकते हैं? – CalZone

+0

@ कैलज़ोन बेशक आप कर सकते हैं। – user3344977

+1

इसका परीक्षण करने के बाद, मुझे एहसास हुआ कि कुछ सामग्री में 'सामग्री स्केल' सेट होने के बावजूद छवि को गलत रिज़ॉल्यूशन के साथ रास्टरराइज किया जाता है। 'Cell.layer.rasterizationScale = [यूआईस्क्रीन मुख्यस्क्रीन]। स्केल सेट करना; 'इसे ठीक करता है – DaGaMs

0

जाओ CustomCollectionviewCell.m करने और जोड़ने के लिए इस प्रयास करें:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     //////// make shadow of total view 
     self.clipsToBounds = NO; 
     self.layer.masksToBounds = NO; 
     self.layer.shadowRadius = 5; 
     self.layer.shadowOpacity = 0.5; 
     self.layer.shadowColor = [UIColor blackColor].CGColor; 
     self.layer.shadowOffset = CGSizeMake(0, 1); 
     self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath; 

     // make radius of the cell 
     self.layer.cornerRadius = 5; 

    } 
    return self; 
} 
संबंधित मुद्दे