【问题标题】:CAShapeLayer problems - not drawing in UITableViewCellCAShapeLayer 问题 - 不在 UITableViewCell 中绘图
【发布时间】:2014-08-22 18:43:54
【问题描述】:

我正在尝试使用CAShapeLayer 在每个具有特定属性的UITableViewCell 上绘制一个实心圆圈(如果布尔属性为真)。我使用CAShapeLayer 的原因是为了以后可以制作动画。

当 tableView 最初被渲染出来时,它可以正常工作。但是,当添加或删除单元格时,会出现问题,严重错误!圆圈被绘制在错误的单元格中,有时在单元格之外(下方)。我不知道出了什么问题。由于在单元格上设置了clipsToBounds,它似乎已经停止在单元格外绘制圆圈,但这仍然不能解决一些被绘制/一些没有被绘制的问题。

以下代码(假定)绘制了一个实心圆。它存在于我的自定义 UITableViewCell 类中:

    let radius = 15
    self.circleLayerForAnimation = CAShapeLayer()

    self.circleLayerForAnimation!.path = UIBezierPath(roundedRect: CGRectMake(CGFloat(0), CGFloat(0), CGFloat(2*radius), CGFloat(2*radius)), cornerRadius: CGFloat(radius)).CGPath
    self.circleLayerForAnimation!.position = CGPointMake(CGRectGetMidX(CGRectMake(self.frame.width - 20.0 - 30.0, 15.0, 30.0, 30.0)) - CGFloat(radius), CGRectGetMidY(self.frame)-CGFloat(radius))
    self.circleLayerForAnimation!.contentsScale = UIScreen.mainScreen().scale

    self.circleLayerForAnimation!.fillColor = UIColor.blueColor().CGColor
    self.circleLayerForAnimation!.strokeColor = UIColor.blueColor().CGColor
    self.circleLayerForAnimation!.lineWidth = 1

    self.contentView.layer.addSublayer(self.circleLayerForAnimation)

在我的表格视图控制器中,我有以下代码可以在 cellForRowAtIndexPath 函数中绘制/删除圆圈:

    if (shouldShowCircle == true) {
        if (cell!.circleLayerForAnimation == nil) {
            cell!.fillCircle() // code above
        }  
    } else {
        if (cell!.circleLayerForAnimation != nil) {
            cell!.circleLayerForAnimation!.removeFromSuperlayer()
            cell!.circleLayerForAnimation = nil
        }  
    }

有人可以在这里帮忙吗,因为这在过去的几个小时里一直让我发疯!

谢谢

【问题讨论】:

  • 你有截图吗?我想这是重复使用单元格时的问题。

标签: ios uitableview swift cashapelayer


【解决方案1】:

尝试在单元格显示时运行动画。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

【讨论】:

  • 是的,我认为这可以解决我的问题 - 我猜是在显示序列期间单元格的帧发生变化。谢谢。
【解决方案2】:

我的问题在于框架。替换:

self.circleLayerForAnimation!.position = CGPointMake(CGRectGetMidX(CGRectMake(self.frame.width - 20.0 - 30.0, 15.0, 30.0, 30.0)) - CGFloat(radius), CGRectGetMidY(self.frame)-CGFloat(radius))

与:

self.circleLayerForAnimation!.position = CGPointMake(CGRectGetMidX(CGRectMake(self.contentView.frame.width - 20.0 - 30.0, 15.0, 30.0, 30.0)) - CGFloat(radius), CGRectGetMidY(self.contentView.frame)-CGFloat(radius))

按预期生成结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多