首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何改变fillColor of UIGraphicsGetCurrentContext?

如何改变fillColor of UIGraphicsGetCurrentContext?
EN

Stack Overflow用户
提问于 2017-07-14 11:25:04
回答 2查看 2K关注 0票数 2

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

代码语言:javascript
复制
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类中,我编写了这个方法

代码语言:javascript
复制
  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)
    }
  }

任何想法!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-14 12:37:59

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

代码语言:javascript
复制
override func draw(_ rect: CGRect) {
    super.draw(rect)
    drawShape(rect: rect, color: self.color!)
}

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

相反,可以这样做(没有测试,只需在这里输入):

代码语言:javascript
复制
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类:

代码语言:javascript
复制
var color: UIColor? {
    didSet {
        self.setNeedsDisplay()
    }
}

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

编辑2:您可以将它粘贴到游乐场页面中,并且它应该不受更改地运行.

代码语言:javascript
复制
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

在操场页面上运行的结果的屏幕记录:

票数 5
EN

Stack Overflow用户

发布于 2017-07-14 12:44:59

就像这样,

代码语言:javascript
复制
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)
    }} 

代码语言:javascript
复制
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
        }
}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45101814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档