UIBezierpath没有绘制平滑的图像

时间:2016-06-08 22:50:31

标签: ios swift uibezierpath

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

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

rendered result: image1

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

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

rendered result: image2

整个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()
        }
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个

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

<强> EDITED

或者您也可以在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函数在原始rect中创建一个rect,

  

返回一个小于或大于源的矩形   矩形,具有相同的中心点。

<强> EDITED 您可以将其放在init中以修复可视化问题

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

我希望这可以帮到你

答案 1 :(得分:1)

你有问题,因为线被剪裁了。当你进入 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)
}