gpt4 book ai didi

ios - 如何更改 UIGraphicsGetCurrentContext 的 fillColor?

转载 作者:行者123 更新时间:2023-12-01 18:05:47 24 4
gpt4 key购买 nike

我编写了这个自定义 UIView 类来绘制自定义形状

class ShapeView: UIView {

var color: UIColor?

init(frame: CGRect, color: UIColor) {
super.init(frame: frame)
self.color = color
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func draw(_ rect: CGRect) {
super.draw(rect)
drawShape(rect: rect, color: self.color!)
}

func drawShape(rect: CGRect,color: UIColor) {
guard let ctx = UIGraphicsGetCurrentContext() else { return }
ctx.setFillColor(UIColor.clear.cgColor)

ctx.beginPath()
ctx.move(to: CGPoint(x: rect.width / 2, y: 0))
ctx.addLine(to: CGPoint(x: rect.width, y: rect.height / 2))
ctx.addLine(to: CGPoint(x: rect.width / 2 , y: rect.height))
ctx.addLine(to: CGPoint(x: 0, y: rect.height / 2))


ctx.closePath()
ctx.setFillColor(color.cgColor)
ctx.fillPath()
ctx.strokePath()
}

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let path = UIBezierPath(ovalIn: self.frame)
return path.contains(point)
}

当用户按下形状内部的某处时,我需要更改此形状的 fillColor,让我们用这段代码说黑色//虽然调试颜色没有改变,但看起来我做错了什么。

在一些 UIViewController 类中我写了这个方法

  class SomeClass: UIViewController {
var shape1: ShapeView?
var frame: CGRect?

override func viewDidLoad() {
let x = view.frame.midX
let y = view.frame.midY

self.frame = CGRect(x: x, y: y, width: 100, height: 100)
super.viewDidLoad()
self.shape1 = ShapeView(frame: frame!, color: .red)
shape1?.backgroundColor = .clear
view.addSubview(shape1!)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let location = touches.first!.location(in: self.view)
if (shape1?.point(inside: location, with: event))! {
print("inside shape1")
shape1?.drawShape(rect: frame!, color: .black)
} else {
print("outside shape1")
shape1?.drawShape(rect: frame!, color: .red)
}
}

任何想法!

最佳答案

不要直接调用您的 drawShape() 函数 - 每次对象绘制自身时都会调用它:

override func draw(_ rect: CGRect) {
super.draw(rect)
drawShape(rect: rect, color: self.color!)
}

因此,它会立即将填充颜色更改回 shape1color 属性。

相反,做这样的事情(未经测试,只需在此处输入):

if (shape1?.point(inside: location, with: event))! {
print("inside shape1")
shape1?.color = UIColor.black
} else {
print("outside shape1")
shape1?.color = UIColor.red
}
shape1?.setNeedsDisplay()

编辑:您还可以像这样修改您的 ShapeView 类:

var color: UIColor? {
didSet {
self.setNeedsDisplay()
}
}

这将消除在触摸处理程序中“手动”调用 .setNeedsDisplay() 的需要。

编辑 #2: 您可以将其粘贴到 Playground 页面中,它应该无需更改即可运行...

import UIKit
import PlaygroundSupport

class ShapeView: UIView {

var color: UIColor? {
didSet {
self.setNeedsDisplay()
}
}

init(frame: CGRect, color: UIColor) {
super.init(frame: frame)
self.color = color
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func draw(_ rect: CGRect) {
super.draw(rect)
drawShape(rect: rect, color: self.color!)
}

func drawShape(rect: CGRect,color: UIColor) {
guard let ctx = UIGraphicsGetCurrentContext() else { return }
ctx.setFillColor(UIColor.clear.cgColor)

ctx.beginPath()
ctx.move(to: CGPoint(x: rect.width / 2, y: 0))
ctx.addLine(to: CGPoint(x: rect.width, y: rect.height / 2))
ctx.addLine(to: CGPoint(x: rect.width / 2 , y: rect.height))
ctx.addLine(to: CGPoint(x: 0, y: rect.height / 2))


ctx.closePath()
ctx.setFillColor(color.cgColor)
ctx.fillPath()
ctx.strokePath()
}

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let path = UIBezierPath(ovalIn: self.frame)
return path.contains(point)
}
}

class VCA : UIViewController {

var shape1: ShapeView?
var frame: CGRect?

override func viewDidLoad() {
let x = view.frame.midX
let y = view.frame.midY

self.frame = CGRect(x: x, y: y, width: 100, height: 100)
super.viewDidLoad()
self.shape1 = ShapeView(frame: frame!, color: .red)
shape1?.backgroundColor = .clear
view.addSubview(shape1!)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let location = touches.first!.location(in: self.view)
if (shape1?.point(inside: location, with: event))! {
print("inside shape1")
shape1?.color = UIColor.black
} else {
print("outside shape1")
shape1?.color = UIColor.red
}
}

}

let vcA = VCA()
vcA.view.backgroundColor = .yellow
PlaygroundPage.current.liveView = vcA.view

在 playground 页面中运行结果的屏幕录像:

enter image description here

关于ios - 如何更改 UIGraphicsGetCurrentContext 的 fillColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45101814/

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