斯威夫特的圈子 - 无法摆脱剪辑

时间:2017-01-30 22:30:30

标签: ios swift uibezierpath

我在Swift 3.0中工作并使用UIBezierPath绘制Circles。我的代码如下。无论我改变什么,圆圈的边缘仍然被修剪。圆圈位于图像视图中。我试过操纵视图的高度/重量,并将其改为"方面填充" "方面适合" "中心"我看过的教程也告诉我,如果我改变" var乘数:CG Float = 1.0"到0.85或其他数字,裁剪将消失。我试过这个并没有用。我该如何解决这个问题?

class Circle1: UIView {
    var multiplier:CGFloat = 0.85
    //this takes away the "clippinh" you might see in the view

    var centerOfCirclesView: CGPoint{
        return CGPoint(x:bounds.midX, y:bounds.midY)
    }
    var halfOfViewsSize: CGFloat{
        return min(bounds.size.height, bounds.size.width) / 2
    }

    var lineWidth:CGFloat = 1.0

    //circle radius compute

    var full = CGFloat(M_PI * 2)
    var quarter = CGFloat(M_PI / 2)
    var half = CGFloat(M_PI)
    var threeQuarters = CGFloat(3 * M_PI / 2)


    func drawCircleCenteredAt(_:CGPoint, withRadius radius:CGFloat) -> UIBezierPath{
        let circlePath = UIBezierPath(arcCenter: centerOfCirclesView,
                                      radius: halfOfViewsSize,
                                      startAngle: 00,
                                      endAngle: full,
                                      clockwise: false)
        circlePath.lineWidth = lineWidth

        return circlePath
    }

    override func draw(_ rect: CGRect) {
        UIColor(hue: 0.7194, saturation: 0, brightness: 0.52, alpha: 1.0).set()
        lineWidth = 15.0
        drawCircleCenteredAt(centerOfCirclesView, withRadius: halfOfViewsSize).stroke()
    }

}

class Circle2: UIView {

    var multiplier:CGFloat = 0.85
    //this takes away the "clippinh" you might see in the view

    var centerOfCirclesView: CGPoint{
        return CGPoint(x:bounds.midX, y:bounds.midY)
    }
    var halfOfViewsSize: CGFloat{
        return min(bounds.size.height, bounds.size.width) / 2
    }

    var lineWidth:CGFloat = 1.0

    //circle radius compute

    var full = CGFloat(M_PI * 2)
    var quarter = CGFloat(M_PI / 2)
    var half = CGFloat(M_PI)
    var threeQuarters = CGFloat(3 * M_PI / 2)


    func drawCircleCenteredAt(_:CGPoint, withRadius radius:CGFloat) -> UIBezierPath{
        let circlePath = UIBezierPath(arcCenter: centerOfCirclesView,
                                      radius: halfOfViewsSize,
                                      startAngle: 00,
                                      endAngle: half,
                                      clockwise: false)
        circlePath.lineWidth = lineWidth

        return circlePath
    }
}

1 个答案:

答案 0 :(得分:0)

您需要减少radius,以便不会裁剪您的圈子。例如,在Circle1中,将halfOfViewSize - 0.5 * lineWidth的{​​{1}}传递给radius,然后在创建drawCircleCenteredAt(_:withRadius:)时使用该半径:

UIBezierPath

您的func drawCircleCenteredAt(_:CGPoint, withRadius radius:CGFloat) -> UIBezierPath { let circlePath = UIBezierPath(arcCenter: centerOfCirclesView, radius: radius, // Changed this to radius startAngle: 0, endAngle: full, clockwise: false) circlePath.lineWidth = lineWidth return circlePath } override func draw(_ rect: CGRect) { UIColor(hue: 0.7194, saturation: 0, brightness: 0.52, alpha: 1.0).set() lineWidth = 15.0 drawCircleCenteredAt(centerOfCirclesView, withRadius: halfOfViewsSize - 0.5 * lineWidth).stroke() } 无效,因为您没有在任何地方使用它。在创建multiplier时,您也可以使用radius来减少halfOfViewSize * multiplier

以下是修改后的代码,用于删除UIBezierPath例程中颜色和线宽的设置。

draw(_:)

这是一个在Swift Playground中运行的演示:

Demo of overlapping circles in Swift Playground