2012-09-26 6 views
10

Dans la nouvelle UICollectionView je ne vois pas comment ajouter une ombre à un UICollectionViewCell. Comment j'irais à ce sujet. Est-ce que j'ajouterais une autre vue?UICollectionViewCell couleur de l'ombre

[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

est-il pas très inefficace d'appeler '[self.collectionView cellForItemAtIndexPath: [self.collectionView indexPathForItemAtPoint: [Recognizer locationInView: [self view]]]] 'plusieurs fois? –

Répondre

1

plus probable que votre problème est mieux résolu avec la réponse existante à How do I draw a shadow under a UIView?

être spécifique à votre situation, vous auriez probablement du code qui ferait ce que le code suivant fait (selon l'endroit où vous obtenez votre CollectionView et someIndexPath afin de pointer vers la cellule qui vous intéresse):

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

il y a évidemment d'autres façons d'obtenir la cellule. la chose importante est la 2ème ligne, pour définir le shadowPath.

+0

Ceci ne crée pas d'ombre autour des éléments, affectant toute la cellule. S'il vous plaît voir ci-dessus. – BDGapps

+0

hmmm ... bien votre question originale dit "* Dans la nouvelle UICollectionView je ne vois pas comment ajouter une ombre à un UICollectionViewCell. *". Si vous voulez placer une ombre autour des éléments dans la cellule, vous devrez collecter les sous-vues de UICollectionViewCell et ensuite effectuer le réglage de l'ombre sur chacun d'eux individuellement. –

0

Vous ne définissez pas la propriété shadowOffset sur le calque.

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

Il ne crée toujours pas l'ombre sur la vue blanche qu'il définit sur la cellule entière. Ce n'est pas une véritable ombre juste un fond transformé. – BDGapps

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

Vous oubliez de mettre masksToBounds sur UIView à NO. Cela devrait fonctionner:

- (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

Est-ce que ça ne va pas ralentir? Toute idée si nous pouvons le faire dans Cell Subclass? – CalZone

+0

@CalZone Bien sûr que vous pouvez. – user3344977

+1

Après avoir testé cela, j'ai réalisé que dans certains cas, l'image est pixellisée avec la mauvaise résolution, malgré la définition de 'contentScale'. Paramètre 'cell.layer.rasterizationScale = [UIScreen mainScreen] .scale;' répare ce – DaGaMs

0

Aller à la CustomCollectionviewCell.m et essayez d'ajouter ceci:

- (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; 
} 
Questions connexes