iOS 方形径向渐变

标签 ios swift gradient

<分区>

如何在正方形中创建径向渐变?

Example square gradient
(来源:graphicxtras.com)

最佳答案

您可以像这样直接在 UIView 子类中绘制径向渐变:

override func draw(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()!
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let colors = [UIColor.red.cgColor, UIColor.green.cgColor] as NSArray
    let gradient = CGGradient(colorsSpace: colorSpace, colors: colors, locations: nil)!
    let center = CGPoint(x: bounds.midX, y: bounds.midY)
    context.drawRadialGradient(gradient, startCenter: center, startRadius: 0, endCenter: center, endRadius: bounds.width/2, options: [.drawsAfterEndLocation])
}

或者简单地检查这个库来绘制任何你想要的开箱即用的渐变 - https://github.com/maxkonovalov/MKGradientView

关于iOS 方形径向渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48683501/

相关文章:

ios - 如何使用一键添加不同整数的分数(swift3)

html - 在浏览器中滚动时,CSS 背景渐变不会覆盖背景

ios - UITextView 取消隐藏后更改 UITableViewCell

ios - Appirater 警报未显示

objective-c - 使用 ViewDeck 不会调用 editingStyleForRowAtIndexPath

使用 NodeJS 的 iOS 推送通知 - 凭据

ios - 在 Swift 中循环遍历嵌入式 JSON 数组?

ios - 了解CAGradient的位置?

android层列表不会同时显示渐变和实体

ios - 如何在 iOS 中保存/恢复 View 状态?