gpt4 book ai didi

ios - 圆角渐变 UIButton iOS

转载 作者:可可西里 更新时间:2023-11-01 05:40:36 29 4
gpt4 key购买 nike

您好,我想创建一个类似于(附加)图像的按钮。

enter image description here

这里的挑战是带有渐变颜色的圆角。我能够使用

创建圆角

view.layer setCornerRadius:radius;view.layer setMasksToBounds:YES;

我也可以创建单一的边框颜色

view.layer.borderColor = color.CGColor; view.layer.borderWidth = 宽度;

但是渐变边框颜色需要做什么呢?

我做了以下操作,但不是我想要的。我关注了这篇文章,但它没有为我提供渐变边框颜色。 UIButton w/ gradient, rounded corners, border, and drop shadow

任何人都可以帮助我理解,我如何为 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)
}
}

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

关于ios - 圆角渐变 UIButton iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32062415/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com