gpt4 book ai didi

ios - NSGradient drawInBezierPath iOS等效

转载 作者:行者123 更新时间:2023-12-01 16:50:16 29 4
gpt4 key购买 nike

是否有一个与- (void)drawInBezierPath:(NSBezierPath *)path angle:(CGFloat)angle等效的iOS设备?

我需要在UIBezierPath内绘制渐变,但无法使其正常工作。渐变会在屏幕上绘制。

最佳答案

本示例在Swift 3的UIBezierPath内部创建一个渐变。它绘制带有绿色/白色渐变的斜线。

import UIKit

class DrawingView: UIView {

override init(frame: CGRect) {
super.init(frame: frame)
self.isOpaque = false
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}


override func draw(_ rect: CGRect) {
let startPoint = CGPoint(x:100, y:100)
let endPoint = CGPoint(x: 300, y:400)

let context = UIGraphicsGetCurrentContext()!
context.setStrokeColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0);
// create a line
context.move(to: startPoint)
context.addLine(to: endPoint)
context.setLineWidth(4)
// use the line created above as a clipping mask
context.replacePathWithStrokedPath()
context.clip()

// create a gradient
let locations: [CGFloat] = [ 0.0, 0.5 ]

let colors = [UIColor.green.cgColor,
UIColor.white.cgColor]

let colorspace = CGColorSpaceCreateDeviceRGB()

let gradient = CGGradient(colorsSpace: colorspace,
colors: colors as CFArray, locations: locations)

let gradientStartPoint = CGPoint(x: rect.midX, y: rect.minY)
let gradientEndPoint = CGPoint(x: rect.midX, y: rect.maxY)

context.drawLinearGradient(gradient!,
start: gradientStartPoint, end: gradientEndPoint,
options: .drawsBeforeStartLocation)
UIGraphicsEndImageContext()
}

}

关于ios - NSGradient drawInBezierPath iOS等效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16674691/

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