UICollectionViewCell阴影颜色

在新的UICollectionView中,我没有看到如何向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; 

在此处输入图像描述

您忘记将UIView上的masksToBounds设置为NO 。 这应该工作:

 - (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; } 
 [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.masksToBounds = NO; 

最有可能你的问题最好用现有的答案来解决我如何在UIView下绘制阴影?

根据您的具体情况,您可能会拥有以下代码所执行的代码(取决于您获取collectionView和someIndexPath的位置以指向您感兴趣的单元格):

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

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

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

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

转到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; }