如何改变UIGraphicsGetCurrentContext的fillColor?

时间:2017-07-14 11:25:04

标签: ios uiview swift3 core-graphics

我已经编写了这个自定义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)
    }
  }

任何想法!

2 个答案:

答案 0 :(得分:3)

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

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

因此,它会立即将填充颜色更改回shape1&#39; 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()
    }
}

这将消除&#34;手动&#34;在你的触摸处理程序中调用.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

屏幕录制在游乐场页面中运行的结果:

enter image description here

答案 1 :(得分:0)

试试这个,

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
        }
}
}