简体   繁体   English

使用CAShapeLayer作为CAShapeLayer的蒙版时的细边框

[英]Thin border when using CAShapeLayer as mask for CAShapeLayer

In Swift, I have two semi-transparent circles, both of which are CAShapeLayer . 在Swift中,我有两个半透明的圆,它们都是CAShapeLayer Since they are semi-transparent, any overlap between them becomes visible like so: 由于它们是半透明的,因此它们之间的任何重叠都是可见的,如下所示:

在此处输入图片说明

Instead, I want them to visually "merge" together. 相反,我希望它们在视觉上“合并”在一起。 The solution I have tried is to use circle 2 as a mask for circle 1, therefore cutting away the overlap. 我尝试过的解决方案是使用圆2作为圆1的蒙版,因此可以消除重叠。

This solution is generally working, but I get a thin line on the outside of circle 2: 该解决方案通常是可行的,但是我在圆圈2的外面得到一条细线:

在此处输入图片说明

My question: How can I get rid of the thin, outside line on the right circle? 我的问题:如何摆脱右圆上的细外线? Why is it even there? 为什么还在那里?


The code is as follows ( Xcode playground can be found here ): 代码如下( 可在此处找到Xcode游乐场 ):

    private let yPosition: CGFloat = 200
    private let circle1Position: CGFloat = 30
    private let circle2Position: CGFloat = 150
    private let circleDiameter: CGFloat = 200
    private var circleRadius: CGFloat { return self.circleDiameter/2.0 }

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .black

        self.view = view

        let circle1Path = UIBezierPath(
            roundedRect: CGRect(
                x: circle1Position,
                y: yPosition,
                width: circleDiameter,
                height: circleDiameter),
            cornerRadius: self.circleDiameter)

        let circle2Path = UIBezierPath(
            roundedRect: CGRect(
                x: circle2Position,
                y: yPosition,
                width: circleDiameter,
                height: circleDiameter),
            cornerRadius: self.circleDiameter)

        let circle1Layer = CAShapeLayer()
        circle1Layer.path = circle1Path.cgPath
        circle1Layer.fillColor = UIColor.white.withAlphaComponent(0.6).cgColor

        let circle2Layer = CAShapeLayer()
        circle2Layer.path = circle2Path.cgPath
        circle2Layer.fillColor = UIColor.white.withAlphaComponent(0.6).cgColor

        self.view.layer.addSublayer(circle1Layer)
        self.view.layer.addSublayer(circle2Layer)

        //Create a mask from the surrounding rectangle of circle1, and
        //then cut out where it overlaps circle2
        let maskPath = UIBezierPath(rect: CGRect(x: circle1Position, y: yPosition, width: circleDiameter, height: circleDiameter))
        maskPath.append(circle2Path)
        maskPath.usesEvenOddFillRule = true
        maskPath.lineWidth = 0

        let maskLayer = CAShapeLayer()
        maskLayer.path = maskPath.cgPath
        maskLayer.fillColor = UIColor.black.cgColor
        maskLayer.fillRule = kCAFillRuleEvenOdd

        circle1Layer.mask = maskLayer
    }

如果两个CAShapeLayers具有相同的alpha值,则可以将它们放在新的父CALayer中,然后设置父代的alpha。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM