2012-09-26 97 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: [识别器locationInView:[self view]]]]'多次? –

回答

1

最有可能您的问题最好用现有的回答解决了How do I draw a shadow under a UIView?

具体到你的情况,你可能有一些代码会做下面的代码做什么(取决于你在哪里得到你的collectionView和someIndexPath以指向你感兴趣的单元):

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

显然有其他方法来获得细胞。重要的是第二行,设置shadowPath。

+0

这不会在影响整个单元的元素周围产生阴影。请参阅上文。 – BDGapps

+0

嗯......以及你原来的问题说:“*在新的UICollectionView我看不到如何添加阴影到UICollectionViewCell。*”。如果要为单元格中的元素放置阴影,则必须收集UICollectionViewCell的子视图,然后分别对每个元素执行阴影设置。 –

0

您没有在图层上设置shadowOffset属性。

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

它仍然不会在它将其设置在整个单元格上的白色视图上创建阴影。它不是一个真正的影子,只是变换的背景。 – BDGapps

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

你忘了对UIView设置masksToBoundsNO。这应该工作:

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

这不会让它变慢吗?任何想法,如果我们可以在Cell Subclass中做到这一点? – CalZone

+0

@CalZone当然可以。 – user3344977

+1

经过测试之后,我意识到在某些情况下,尽管设置了“contentScale”,图像仍会以错误的分辨率进行栅格化。设置'cell.layer.rasterizationScale = [UIScreen mainScreen] .scale;'修复此问题 – 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; 
}