【问题标题】:Border in circle UIView using Swift使用 Swift 的圆形 UIView 中的边框
【发布时间】:2015-08-20 00:28:19
【问题描述】:

我正在尝试制作一个带边框的 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()

    }
}

我使用了代码:

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()

}

但我得到了一个带有一点切口的边框:

【问题讨论】:

  • 不清楚你想要达到的目标。
  • @matt 我改进了这个问题,看看你现在能不能理解

标签: ios swift uiview uibezierpath


【解决方案1】:

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

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

    }

【讨论】:

    【解决方案2】:
    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()
    
        }
    

    但请注意,要使边框起作用,您需要在圆圈周围留出一些空间。

    执行以下操作。声明myBorderWidthCGFloat类型)属性,然后更改

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

    static var p = UIBezierPath(ovalInRect: CGRectMake(myBorderWidth/2,myBorderWidth/2,118-myBorderWidth/2,117-myBorderWidth/2))
    

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

    【讨论】:

    • 在我的问题更新中查看结果,它削减了边界的某些部分
    • @marchiore 边框的宽度是从你的圆的边缘计算的。所以,如果你放 10 它将严重削减它们,因为更远的 5 点将在你的 uiview 之外。你需要确保你的圈子周围有一些空间
    【解决方案3】:

    无需修改图层,只需根据边框厚度设置 UIBezierPath 并设置适当的插图并设置其宽度(在 Swift 4.2 上测试):

    var fillColor: UIColor //circle color
    var strokeColor: UIColor //border color
    var borderWidth: CGFloat //border width
    
    override func draw(_ rect: CGRect) {
        fillColor.setFill()
        strokeColor.setStroke()
    
        let circlePath = UIBezierPath(ovalIn: CGRect(x: borderWidth, y: borderWidth, width: rect.width - borderWidth*2, height: rect.height - borderWidth*2))
        circlePath.lineWidth = borderWidth
    
        circlePath.stroke()
        circlePath.fill()
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      相关资源
      最近更新 更多