UITableViewCell中的CALayer阴影绘制不正确 [英] CALayer shadow in UITableViewCell Drawn incorrectly

查看:129
本文介绍了UITableViewCell中的CALayer阴影绘制不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CALayer将阴影应用于UITableViewCell。

I am applying shadow to a UITableViewCell using CALayer.

这是我的代码:

- (void)addShadowToView:(UIView *)view
{
    // shadow
    view.layer.shadowColor = [[UIColor colorWithWhite:0.0f alpha:0.1f] CGColor];
    view.layer.shadowOpacity = 1.0f;
    view.layer.shadowOffset = CGSizeMake(0.0f, 3.0f);
    view.layer.shadowRadius = 6.0f;

    CGRect shadowFrame = view.layer.bounds;
    CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
    view.layer.shadowPath = shadowPath;
}

问题是对于某些tableviewcells,阴影不会跨越整个宽度细胞对于某些细胞来说,这是正确的,对于其他细胞则是错误的。我注意到设备的旋转也会影响它,并且重新加载tableview数据有时会解决它。

The issue is that for some tableviewcells, the shadow does not span the entire width of the cell. For some cells it would be correct, for others it would be faulty. I do notice that the rotation of the device also affects it, and reloading of the tableview data sometimes solves it.

缓解此问题的最佳方法是什么(以及我并不是要在每次轮换等时重新加载整个tableview。)?

What is the best way to mitigate this issue (and with that I don't mean to reload the whole tableview on each rotation etc.)?

正确应用阴影的单元格底部示例:

Example bottom of cell where shadow is correctly applied:

向下滚动后同一桌面视图中的单元格底部(阴影仅应用于宽度的前75%):

Bottom of cell in same tableview after scrolling down (shadow only applied for first 75% of width):

编辑我注意到问题是由这些代码行引起的:

I have noticed the issue is caused from these lines of code:

CGRect shadowFrame = view.layer.bounds;
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
view.layer.shadowPath = shadowPath;

如果我把它们遗漏,一切都很好。但是我被告知使用它时会有一定的性能优势。不知何故,旋转后阴影没有正确应用于新尺寸..

If I leave them out, everything is fine. But I've been told there is certain performance benefit when using this. Somehow the shadow is not correctly applied to new dimensions after rotating..

推荐答案

你可以为你的细胞框架覆盖setter并调用 addShadowToView:。您可以通过存储单元格的大小来更优化这一点,并且仅在大小更改时更新阴影路径,例如:

You can override the setter for you're cell's frame and call addShadowToView:. You can optimize this more by storing your cell's size and only updating the shadow path when the size changes for example:

@property (nonatomic, assign) CGSize size;

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    // Need to check make sure this subview has been initialized
    if(self.subviewThatNeedsShadow != nil && !CGSizeEqualToSize(self.size,_frame.size)
    {
        [self addShadowToView: self.subviewThatNeedsShadow];
    }
}

这篇关于UITableViewCell中的CALayer阴影绘制不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆