UIBezierpath 没有绘制平滑的图像

UIBezierpath is not drawing a smooth image

我想在 swift 中创建一个自定义按钮,结果不太令人满意。 UIBezierpath 未绘制平滑路径。请看下面的图片和代码。

let roundRect = UIBezierPath(roundedRect: CGRectMake(0, 0, 156, 60), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))

稍微玩了一下,我发现将路径向下和向右移动1点并将矩形尺寸缩小1就可以解决问题。但我不喜欢这个解决方案,我认为这不是正确的做法。而且我觉得框架正在剪裁我的图像。有人可以帮我解决这个问题吗??

let roundRect = UIBezierPath(roundedRect: CGRectMake(1, 1, 154, 59), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))

整个 CustomButton 子类:

import UIKit

class DesignableButtons: UIButton {
    let black = UIColor(red: 33/255, green: 33/255, blue: 33/255, alpha: 1.0) /* #212121 */

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.setTitle("Go to Google", forState: UIControlState.Normal)
        self.titleLabel!.font = UIFont(name: "GT Walsheim", size: 14)
        self.setTitleColor(black, forState: .Normal)
        self.setTitleColor(UIColor.whiteColor(), forState: .Highlighted)
        self.setTitleColor(UIColor.whiteColor(), forState: .Selected)
        self.titleLabel!.alpha = 1.0
    }

    let roundRect = UIBezierPath(roundedRect: CGRectMake(0, 0, 156, 60), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))

    override func drawRect(rect: CGRect) {
        black.setStroke()
        roundRect.stroke()
        if highlighted {
            black.setFill()
            roundRect.fill()
        }
    }

    override var highlighted: Bool {
        didSet {
            setNeedsDisplay()
        }
    }
}

试试这个

let roundRect = UIBezierPath(roundedRect: CGRectInset(CGRectMake(0, 0, 156, 60), 1, 1), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))

已编辑

或者您也可以这样做以提高可重用性,这在您的 layoutSubviews

let roundRect = UIBezierPath(roundedRect: CGRectInset(self.frame, 1, 1), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))

而不是

let roundRect = UIBezierPath(roundedRect: CGRectMake(0, 0, 156, 60), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))

CGRectInset 函数在您的原始矩形内创建一个矩形,

Returns a rectangle that is smaller or larger than the source rectangle, with the same center point.

已编辑 你可以把它放在你的 init 中来解决可视化问题

self.contentScaleFactor = UIScreen.mainScreen().scale //SUPPORT RETINA

希望对你有所帮助

你有问题,因为线条被剪掉了。当您绘制 drawRect: 时,这种情况总是会发生。尝试在 drawRect:.

外部绘图

例如:

required init?(coder aDecoder: NSCoder) {
    // some code
    layer.masksToBounds = true
    layer.cornerRadius = 200
    layer.borderWidth = 1
    layer.borderColor = black.CGColor
}

或者如果你想使用子层:

let borderLine = CAShapeLayer()

required init?(coder aDecoder: NSCoder) {
    // some code
    frame = someFrame

    let roundRect = UIBezierPath(roundedRect: CGRectMake(0, 0, 156, 60), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(200, 200))
    borderLine.path = roundRect.CGPath
    borderLine.frame = bounds
    borderLine.strokeColor = black.CGColor
    borderLine.fillColor = UIColor.clearColor().CGColor
    borderLine.lineWidth = 1

    layer.insertSublayer(borderLine, atIndex: 0)
}