UICollectionViewCell阴影颜色

BDG*_*pps 10 ios uicollectionview

在新的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;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

mar*_*ram 42

你忘了设定masksToBoundsUIViewNO.这应该工作:

- (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;
}
Run Code Online (Sandbox Code Playgroud)


joh*_*doe 1

最有可能的是,您的问题最好通过现有的答案来解决:如何在 UIView 下绘制阴影?

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

    UICollectionViewCell* collectionViewCell
      = [collectionView dequeueReusableCellWithReuseIdentifier:DEFINED_IDENTIFIER forIndexPath:someIndexPath];
    collectionViewCell.layer.shadowPath = [UIBezierPath bezierPathWithRect:collectionViewCell.bounds].CGPath;
Run Code Online (Sandbox Code Playgroud)

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