我编写了这个自定义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)
    }
  }任何想法!
发布于 2017-07-14 12:37:59
不要直接调用您的drawShape() func --每次对象绘制自己时都会调用它:
override func draw(_ rect: CGRect) {
    super.draw(rect)
    drawShape(rect: rect, color: self.color!)
}因此,它将立即将填充颜色更改为shape1的color属性。
相反,可以这样做(没有测试,只需在这里输入):
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()
    }
}这将消除在touches处理程序中“手动”调用.setNeedsDisplay()的需要。
编辑2:您可以将它粘贴到游乐场页面中,并且它应该不受更改地运行.
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在操场页面上运行的结果的屏幕记录:

发布于 2017-07-14 12:44:59
就像这样,
import UIKit
class ShapeView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
    }
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        drawShape(rect: rect)
    }
    func drawShape(rect: CGRect) {
        guard let ctx = UIGraphicsGetCurrentContext() else
        { return }
        ctx.setFillColor(UIColor.red.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.fillPath()
        ctx.strokePath()
    }
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let path = UIBezierPath(ovalIn: self.frame)
        return path.contains(point)
    }} 和
class SomeClass: UIViewController {
    var shape1: ShapeView?
    var frame: CGRect?
    override func viewDidLoad() {
        super.viewDidLoad()
        let x = view.frame.midX
        let y = view.frame.midY
         shape1 = ShapeView(frame: CGRect(x: x, y: y, width: 100, height: 100))
        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?.backgroundColor = .green
        } else {
            print("outside shape1")
            shape1?.backgroundColor = .black
        }
}
}https://stackoverflow.com/questions/45101814
复制相似问题