CALayer的一些视觉错误

时间:2017-02-10 02:30:32

标签: ios uiimageview calayer uibezierpath cashapelayer

我有一个带有以下单元格布局的UITableView:

•UIView

••UIImageView

•••半透明叠加UIView作为UIImageView的子视图

还有图标视图模仿"播放"按钮和UILabel,但它们似乎与此错误无关。

我有以下代码:

override func layoutSubviews() {
        super.layoutSubviews()

        createPath()
    }

    private func createPath() {
        let padding = CGFloat(16)
        let bottomLineLength = CGFloat(64)
        let arcPadding = CGFloat(8)

        let width = self.contentView.frame.width
        let height = self.contentView.frame.height

        if pathLayer != nil {
            pathLayer?.removeFromSuperlayer()
        }

        pathLayer = CAShapeLayer()

        let path = UIBezierPath()

        path.move(to: CGPoint(x: padding + arcPadding, y: padding))
        path.addLine(to: CGPoint(x: width - padding - arcPadding, y: padding))

        // Top-Right arc
        path.addArc(withCenter: CGPoint(x: width - padding - arcPadding, y: padding + arcPadding),
                    radius: arcPadding,
                    startAngle: CGFloat(3 * M_PI / 2),
                    endAngle: CGFloat(M_PI * 2),
                    clockwise: true)

        path.move(to: CGPoint(x: width - padding, y: padding + arcPadding))
        path.addLine(to: CGPoint(x: width - padding, y: height - padding - arcPadding))

        // Bottom-Right arc
        path.addArc(withCenter: CGPoint(x: width - padding - arcPadding, y: height - padding - arcPadding),
                    radius: arcPadding,
                    startAngle: CGFloat(M_PI * 2),
                    endAngle: CGFloat(M_PI / 2),
                    clockwise: true)
        path.move(to: CGPoint(x: width - padding - arcPadding, y: height - padding))
        path.addLine(to: CGPoint(x: width - padding - bottomLineLength, y: height - padding))

        path.move(to: CGPoint(x: padding + bottomLineLength, y: height - padding))
        path.addLine(to: CGPoint(x: padding + arcPadding, y: height - padding))

        // Bottom-Left arc
        path.addArc(withCenter: CGPoint(x: padding + arcPadding, y: height - padding - arcPadding),
                    radius: arcPadding,
                    startAngle: CGFloat(M_PI / 2),
                    endAngle: CGFloat(M_PI),
                    clockwise: true)

        path.move(to: CGPoint(x: padding, y: height - padding - arcPadding))
        path.addLine(to: CGPoint(x: padding, y: padding + arcPadding))

        // Top-Left arc
        path.addArc(withCenter: CGPoint(x: padding + arcPadding, y: padding + arcPadding),
                    radius: arcPadding,
                    startAngle: CGFloat(M_PI),
                    endAngle: CGFloat(3 * M_PI / 2),
                    clockwise: true)

        pathLayer?.rasterizationScale = 2 * UIScreen.main.scale
        pathLayer?.shouldRasterize = true
        pathLayer?.strokeColor = UIColor(red: 0xFF/255, green: 0xFF/255, blue: 0xFF/255, alpha: 0.5).cgColor
        pathLayer?.lineWidth = 2.0
        pathLayer?.path = path.cgPath

        contentView.layer.addSublayer(pathLayer!)
    }

我在layoutSubviews中调用它来重新创建设备轮换路径。

如果你看截图,有一些黑色和旋转有点黑色"孔"靠近小路。所有这些看起来都一样,它们肯定不会出现在UIImageView中的图像上。

Screenshot

我认为这与我的CALayer有关。如何解决?

1 个答案:

答案 0 :(得分:3)

你的问题是这些黑色楔子:

black wedges

根本原因是fillColor的默认CAShapeLayer是不透明的黑色。将其设置为nil以避免填充:

pathLayer?.fillColor = nil

完成后,让我向您展示一条构建路径的简短方法:

let cgPath = CGMutablePath()
let start = CGPoint(x: padding + bottomLineLength, y: height - padding)
let blCorner = CGPoint(x: padding, y: height - padding)
let tlCorner = CGPoint(x: padding, y: padding)
let trCorner = CGPoint(x: width - padding, y: padding)
let brCorner = CGPoint(x: width - padding, y: height - padding)
let end = CGPoint(x: width - padding - bottomLineLength, y: height - padding)

cgPath.move(to: start)
cgPath.addArc(tangent1End: blCorner, tangent2End: tlCorner, radius: arcPadding)
cgPath.addArc(tangent1End: tlCorner, tangent2End: trCorner, radius: arcPadding)
cgPath.addArc(tangent1End: trCorner, tangent2End: brCorner, radius: arcPadding)
cgPath.addArc(tangent1End: brCorner, tangent2End: end, radius: arcPadding)
cgPath.addLine(to: end)

shapeLayer.path = cgPath

结果:

no wedges