在子类值中使用 uislider 更改线宽
Change line width with uislider in a subclass value
我的代码试图使用幻灯片值来更改线条的宽度。现在它不起作用。我只希望不希望在值更改之前绘制的线条仅在 class Canvas 中的增值税编号为 changed.Look 之后的新行生效。 Struct ColoredLine 控制线条的颜色。
struct ColoredLine {
var color = UIColor.black
var points = [CGPoint]()
}
class ViewController: UIViewController {
@objc func hhh() {
canvas.number = Int(justinBiber.value)
}
var justinBiber = UISlider()
}
class Canvas: UIView {
var strokeColor = UIColor.green
var number = 5
func undo() {
_ = lines.popLast()
setNeedsDisplay()
}
func clear() {
lines.removeAll()
setNeedsDisplay()
}
var lines = [ColoredLine]()
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
context.setLineWidth(number)
context.setLineCap(.butt)
lines.forEach { (line) in
for (i, p) in line.points.enumerated() {
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
}
context.setStrokeColor(line.color.cgColor)
context.strokePath()
context.beginPath()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
var coloredLine = ColoredLine()
coloredLine.color = strokeColor
lines.append(coloredLine)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else { return }
guard var lastLine = lines.popLast() else { return }
lastLine.points.append(point)
lines.append(lastLine)
setNeedsDisplay()
}
}
线宽只是你线的另一个属性。将 属性 添加到 ColoredLine
struct
:
struct ColoredLine {
var color = UIColor.black
var width = 5
var points = [CGPoint]()
}
将 strokeWidth
属性 添加到您的 Canvas
class 并在滑块值更改时更新它:
class Canvas : UIView {
var strokeWidth = 5
....
}
在touchesBegan()
中,将strokeWidth
的当前值添加到coloredLine
实例中:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
var coloredLine = ColoredLine()
coloredLine.color = strokeColor
coloredLine.width = strokeWidth
lines.append(coloredLine)
}
然后在draw(rect:)
画线前设置context
的strokeWidth
:
lines.forEach { (line) in
for (i, p) in line.points.enumerated() {
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
}
context.setStrokeColor(line.color.cgColor)
context.setLineWidth(line.width)
context.strokePath()
context.beginPath()
}
我的代码试图使用幻灯片值来更改线条的宽度。现在它不起作用。我只希望不希望在值更改之前绘制的线条仅在 class Canvas 中的增值税编号为 changed.Look 之后的新行生效。 Struct ColoredLine 控制线条的颜色。
struct ColoredLine {
var color = UIColor.black
var points = [CGPoint]()
}
class ViewController: UIViewController {
@objc func hhh() {
canvas.number = Int(justinBiber.value)
}
var justinBiber = UISlider()
}
class Canvas: UIView {
var strokeColor = UIColor.green
var number = 5
func undo() {
_ = lines.popLast()
setNeedsDisplay()
}
func clear() {
lines.removeAll()
setNeedsDisplay()
}
var lines = [ColoredLine]()
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
context.setLineWidth(number)
context.setLineCap(.butt)
lines.forEach { (line) in
for (i, p) in line.points.enumerated() {
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
}
context.setStrokeColor(line.color.cgColor)
context.strokePath()
context.beginPath()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
var coloredLine = ColoredLine()
coloredLine.color = strokeColor
lines.append(coloredLine)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else { return }
guard var lastLine = lines.popLast() else { return }
lastLine.points.append(point)
lines.append(lastLine)
setNeedsDisplay()
}
}
线宽只是你线的另一个属性。将 属性 添加到 ColoredLine
struct
:
struct ColoredLine {
var color = UIColor.black
var width = 5
var points = [CGPoint]()
}
将 strokeWidth
属性 添加到您的 Canvas
class 并在滑块值更改时更新它:
class Canvas : UIView {
var strokeWidth = 5
....
}
在touchesBegan()
中,将strokeWidth
的当前值添加到coloredLine
实例中:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
var coloredLine = ColoredLine()
coloredLine.color = strokeColor
coloredLine.width = strokeWidth
lines.append(coloredLine)
}
然后在draw(rect:)
画线前设置context
的strokeWidth
:
lines.forEach { (line) in
for (i, p) in line.points.enumerated() {
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
}
context.setStrokeColor(line.color.cgColor)
context.setLineWidth(line.width)
context.strokePath()
context.beginPath()
}