不希望bezier路径被视图框剪裁

时间:2016-02-19 10:26:04

标签: ios swift

除非明确指出,否则我认为Bezier路径不会被包含视图剪切。我已在故事板中验证过,并且可以确认"剪辑子视图"未经检查。我还在clipsToBounds = false

中添加了drawRect()

有没有办法不在包含视图的边缘剪切bezier路径?

我不想要像图片中所示的修剪圆圈:

enter image description here

这是我的代码:

override func drawRect(rect: CGRect) {

    let lineWidth: CGFloat = 6
    let color: UIColor = UIColor.yellowColor()    
    clipsToBounds = false

    let haloRectFrame = CGRectMake(0, 0, bounds.width, bounds.height)
    let haloPath = UIBezierPath(ovalInRect: haloRectFrame)
    haloPath.lineWidth = lineWidth
    color.set()
    haloPath.stroke() 

1 个答案:

答案 0 :(得分:0)

好的,我已经实施了两个解决方案。第一种解决方案利用Bezier路径和行程。在这里,笔划得到了'剪裁,所以要修复它我减少了框架尺寸。第二种解决方案使用CAShapeLayer,它是CALayer的子类。这里我使用Bezier路径,但由于CAShapeLayer没有剪裁,我不需要减小帧大小。

解决方案1(中风的Bezier路径):

override func drawRect(rect: CGRect) {
    let haloPath = UIBezierPath(ovalInRect: rect).CGPath
    let haloLayer = CAShapeLayer(layer: layer)
    haloLayer.path = haloPath
    haloLayer.fillColor = UIColor.clearColor().CGColor
    haloLayer.strokeColor = color.CGColor
    haloLayer.lineWidth = lineWidth
    layer.addSublayer(haloLayer)
}

解决方案2(CAShapeLayer):

let usernamesA = (data["A"]! as NSArray).valueForKey("username")
let usernamesB = (data["B"]! as NSArray).valueForKey("username")