Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中相交于层_Ios_Swift_Cashapelayer_Cgrect - Fatal编程技术网

如何在iOS中相交于层

如何在iOS中相交于层,ios,swift,cashapelayer,cgrect,Ios,Swift,Cashapelayer,Cgrect,我有一个加号(+),当前是蓝色的,但我想让它透明,这样用户就可以看到背景。加号图层将添加到更大的视图中。将加号图层设置为clearcolor并不能解决问题 class AddButtonView: UIView { ... private func setupPlusLayer() { let path = UIBezierPath() path.move(to: CGPoint(x: plusButton.frame.midX, y: plus

我有一个加号(+),当前是蓝色的,但我想让它透明,这样用户就可以看到背景。加号图层将添加到更大的视图中。将加号图层设置为
clear
color并不能解决问题

class AddButtonView: UIView {
    ...

    private func setupPlusLayer() {
        let path = UIBezierPath()
        path.move(to: CGPoint(x: plusButton.frame.midX, y: plusButton.frame.midY-20))
        path.addLine(to: CGPoint(x: plusButton.frame.midX, y: plusButton.frame.midY+20))
        path.move(to: CGPoint(x: plusButton.frame.midX-20, y: plusButton.frame.midY))
        path.addLine(to: CGPoint(x: plusButton.frame.midX+20, y: plusButton.frame.midY))
        path.usesEvenOddFillRule = true

        let shapeLayer = CAShapeLayer()
        shapeLayer.fillRule = .evenOdd
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor = UIColor.blue.cgColor
        shapeLayer.fillColor = UIColor.blue.cgColor
        shapeLayer.lineWidth = 4

        // Add that `CAShapeLayer` to your view's layer:
        self.layer.addSublayer(shapeLayer)
    }
}


如何使加号透明?

尝试使用带+的.png图像使其透明。这样可以很好地工作,您无需绘制加号。

您可以通过以下方式实现此目的:

  • 创建圆
    CAShapeLayer
    circleShape
  • 创建与圆圈形状相同颜色的反转遮罩层(
    反转
    )。对于这种情况,您需要一个具有完全相同的圆路径和加号路径的
    CGMutablePath
    。另外,不要忘记设置
    inversed.fillRule=.evenOdd
  • 你需要一个只带透明加号的图层(
    plusShape
  • 添加
    圆形
    作为视图层的子层
  • 设置掩码:
    circleShape.mask=inversed
  • plusShape
    作为子层添加到视图的层中
  • 就这样!现在你有了透明加唱歌。例如:


    能够通过编程方式绘制加号以节省空间并使应用程序具有分辨率,这将是非常酷的。这将是非常酷的,但在我看来,如果没有这样好的结果,这将是大量的工作。加号按钮层是什么?也许应该是self.layer添加shapelayer@agibson007谢谢,编辑