使用Swift在圈子UIView中的边框

mar*_*ore 4 uiview ios uibezierpath swift

我正在尝试用边框制作UIView,但我只是让它出现在UIView中,而不仅仅是在圆圈上.

样品

在棕色圆圈中我想要一个黑色边框,但它必须在UIView中不在圆圈中.

我有一个名为Ball的课,我画圆圈.

class Ball: UIView {

    var desiredColour = UIColor.blueColor()

    struct mine {
        static var p = UIBezierPath(ovalInRect: CGRectMake(0,0,118,117))

    }

    override func drawRect(rect: CGRect) {
        // Drawing code


        desiredColour.setFill()
        mine.p.fill()

    }


    func colour() {

        var randColor: UIColor = Colors.randomColor()

        Colors.ballColor = randColor
        Colors.colorPosition = find(Colors.arrayColors, randColor)!

        desiredColour = randColor
        self.setNeedsDisplay()

    }
}
Run Code Online (Sandbox Code Playgroud)

我用过代码:

override func drawRect(rect: CGRect) {
    // Drawing code


    desiredColour.setFill()

    let desiredBorderColor = UIColor.blackColor()
    desiredBorderColor.setStroke()

    self.layer.borderWidth  = 3.0
    self.layer.cornerRadius = self.frame.size.width/2.0

    mine.p.fill()
    mine.p.stroke()

}
Run Code Online (Sandbox Code Playgroud)

但我得到一个小切口的边框:

在此输入图像描述

Nis*_*ant 8

尝试通过将视图作为参数传递来调用此函数

func drawBlackBorder(view: UIView) {
        view.layer.borderColor = UIColor.blackColor
        view.layer.borderWidth = 1.0

        view.layer.cornerRadius = view.frame.size.width/2.0
       view.backgroundColor = UIColor.brownColor

    }
Run Code Online (Sandbox Code Playgroud)


And*_*huk 5

override func drawRect(rect: CGRect) {
        // Drawing code


        desiredColour.setFill()

        let desiredBorderColor = UIColor.blackColor()
        desiredBorderColor.setStroke()

        mine.p.lineWidth = 2.0 //set to whatever you want

        mine.p.fill()
        mine.p.stroke()

    }
Run Code Online (Sandbox Code Playgroud)

但请注意,对于工作边界,您需要在您周围留出一些空间.

请执行下列操作.声明myBorderWidth(类型CGFloat)属性然后更改

static var p = UIBezierPath(ovalInRect: CGRectMake(0,0,118,117))
Run Code Online (Sandbox Code Playgroud)

static var p = UIBezierPath(ovalInRect: CGRectMake(myBorderWidth/2,myBorderWidth/2,118-myBorderWidth/2,117-myBorderWidth/2))
Run Code Online (Sandbox Code Playgroud)

您还可以myBorderWidth/2通过将其声明为属性来删除重复项.