2012-09-26 25 views
10

En el nuevo UICollectionView no veo cómo agregar una sombra a una UICollectionViewCell. ¿Cómo voy a hacer esto? ¿Añadiría otra vista?Color de sombra 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

No es muy ineficiente para llamar '[self.collectionView cellForItemAtIndexPath: [self.collectionView indexPathForItemAtPoint: [recognizer locationInView: [self view]]]] 'varias veces? –

Respuesta

1

más probable es que su problema se resuelve mejor con la respuesta existente para How do I draw a shadow under a UIView?

ser específico a sus circunstancias, es probable que tenga código que hacer lo que hace el siguiente código (según dónde consiga su collectionView y someIndexPath para apuntar a la celda que le interese):

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

obviamente hay otras formas de obtener la celda. lo importante es la 2da línea, para establecer el shadowPath.

+0

Esto no crea una sombra alrededor de los elementos, ya que afecta a toda la celda. Por favor mira arriba. – BDGapps

+0

hmmm ... bueno, tu pregunta original decía "* En el nuevo UICollectionView no veo cómo agregar una sombra a un UICollectionViewCell. *". si desea colocar una sombra alrededor de los elementos en la celda, deberá recopilar las subvistas de UICollectionViewCell y luego realizar la configuración de la sombra en cada una individualmente. –

0

No está configurando la propiedad shadowOffset en la capa.

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

Todavía no está creando la sombra en la vista blanca que establece en toda la celda. No es una verdadera sombra solo un trasfondo transformado. – BDGapps

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

Se olvida de establecer masksToBounds en UIView a NO. Esto debería funcionar:

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

¿Esto no lo ralentizará? ¿Alguna idea de si podemos hacerlo en Cell Subclass? – CalZone

+0

@ Calzone Por supuesto que puedes. – user3344977

+1

Después de probar esto, me di cuenta de que, en algunos casos, la imagen se rasteriza con la resolución incorrecta, a pesar de que se configura el 'contentScale'. Configuración 'cell.layer.rasterizationScale = [UIScreen mainScreen] .scale;' corrige esto – DaGaMs

0

Ir a la CustomCollectionviewCell.m y tratar de añadir lo siguiente:

- (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; 
} 
Cuestiones relacionadas