嗨,我想创建一个类似于(附加)图像的按钮。
这里的挑战是带有渐变颜色的圆角。我能够使用创建圆角
view.layer setCornerRadius:radius; view.layer setMasksToBounds:YES;
我也可以创建单边框颜色
view.layer.borderColor = color.CGColor; view.layer.borderWidth = 宽度;
但是渐变边框颜色需要做些什么呢?
我做了以下但它不是我想要的。我关注了这篇文章,但它没有为我提供渐变边框颜色。 带有渐变、圆角、边框和阴影的 UIButton
任何人都可以帮助我理解,我如何为 UIButton 绘制渐变边框。伪代码/示例将非常有帮助。
这通过创建一个在中间切开孔的多边形圆形矩形,然后使用它来剪辑线性渐变的填充,从而将线性渐变应用于圆形矩形周围的线条。
class DisplayView : UIView {
override func drawRect(var rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)
// Inset the rect so we can stroke it and not be outside the button bounds
rect = CGRectInset(rect, 4.0, 4.0)
// Construct the the bounds
var path = CGPathCreateWithRoundedRect(rect, rect.size.height / 2, rect.size.height / 2, nil)
// Fill the middle with white (or other background color
CGContextAddPath(context, path)
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillPath(context)
// stroke the path
path = CGPathCreateCopyByStrokingPath(path, nil, 4.0, kCGLineCapButt, kCGLineJoinBevel, 0)
// Add the outer edge of the button frame
CGContextAddPath(context, path)
// Create a gradient from white to red
let colors = [
UIColor.yellowColor().CGColor,
UIColor.redColor().CGColor
]
// Set the round rect as the clip
CGContextClip(context)
// Fill the path with the radial gradient
let baseSpace = CGColorSpaceCreateDeviceRGB();
let gradient = CGGradientCreateWithColors(baseSpace, colors, nil);
CGContextDrawLinearGradient(
context,
gradient,
CGPoint(x:0, y:0),
CGPoint(x:CGRectGetMaxX(rect), y:CGRectGetMaxY(rect)),
.allZeros)
// Fill the line
CGContextFillPath(context)
CGContextRestoreGState(context)
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特 4
class DisplayView : UIView {
override func draw(_ rect: CGRect) {
var rect = rect
let context = UIGraphicsGetCurrentContext()
context!.saveGState()
// Inset the rect so we can stroke it and not be outside the button bounds
rect = rect.insetBy(dx: 4.0, dy: 4.0)
// Construct the the bounds
var path = CGPath(roundedRect: rect,
cornerWidth: rect.size.width / 2,
cornerHeight: rect.size.height / 2,
transform: nil)
// Fill the middle with white (or other background color
context!.addPath(path)
context?.setFillColor(UIColor.clear.cgColor)
context?.fillPath()
// stroke the path
path = path.copy(strokingWithWidth: 4.0,
lineCap: CGLineCap.butt,
lineJoin: CGLineJoin.bevel,
miterLimit: 0)
// Add the outer edge of the button frame
context?.addPath(path)
// Create a gradient from white to red
let colors = [
UIColor.yellow.cgColor,
UIColor.red.cgColor
]
// Set the round rect as the clip
context?.clip()
// Fill the path with the radial gradient
let baseSpace = CGColorSpaceCreateDeviceRGB();
let gradient = CGGradient(colorsSpace: baseSpace,
colors: colors as CFArray,
locations: nil);
context?.drawLinearGradient(gradient!,
start: CGPoint(x:0, y:0),
end: CGPoint(x: rect.maxX, y: rect.maxY),
options: CGGradientDrawingOptions())
// Fill the line
context?.fillPath()
context?.restoreGState()
}
}
Run Code Online (Sandbox Code Playgroud)