2012-09-26 8 views
10

Nel nuovo UICollectionView non vedo come aggiungere un'ombra a UICollectionViewCell. Come andrei su questo. Dovrei aggiungere un'altra vista?UICollectionViewCell shadow color

[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

Non è molto inefficiente per chiamare '[self.collectionView cellForItemAtIndexPath: [self.collectionView indexPathForItemAtPoint: [recognizer locationInView: [self view]]]] 'più volte? –

risposta

1

molto probabilmente il problema è meglio risolto con la risposta esistente per How do I draw a shadow under a UIView?

di essere specifico per il vostro circostanza, si sarebbe probabilmente avere il codice che avrebbe fatto quello che il seguente codice fa (a seconda di dove si ottiene la tua collezioneView e someIndexPath per puntare alla cella che ti interessa):

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

ci sono ovviamente altri modi per ottenere la cella. l'importante è la seconda linea, per impostare shadowPath.

+0

Questo non sta creando un'ombra attorno agli elementi che riguardano l'intera cella. Si prega di vedere sopra. – BDGapps

+0

hmmm ... beh, la tua domanda iniziale diceva "* Nel nuovo UICollectionView non vedo come aggiungere un'ombra a UICollectionViewCell. *". se si desidera posizionare un'ombra attorno agli elementi nella cella, sarà necessario raccogliere le sottoview di UICollectionViewCell e quindi eseguire l'impostazione dell'ombra su ciascuna di esse individualmente. –

0

Non si imposta la proprietà shadowOffset sul livello.

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

Ancora non sta creando l'ombra sulla vista bianca che imposta su tutta la cella. Non è una vera ombra solo uno sfondo trasformato. – BDGapps

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

Stai dimenticando di impostare masksToBounds su UIView a NO. Questo dovrebbe funzionare:

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

Non rallenterà questo? Qualche idea se possiamo farlo in Cell Subclass? – CalZone

+0

@CalZone Ovviamente è possibile. – user3344977

+1

Dopo aver provato questo, mi sono reso conto che in alcuni casi l'immagine viene rasterizzata con la risoluzione sbagliata, nonostante sia stato impostato 'contentScale'. Impostando 'cell.layer.rasterizationScale = [UIScreen mainScreen] .scale;' risolve questo problema – DaGaMs

0

Vai alla CustomCollectionviewCell.m e cercare di aggiungere questo:

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