【问题标题】:context.fill() doesn't show up Swift 4 Xcode 10context.fill() 没有显示 Swift 4 Xcode 10
【发布时间】:2019-07-21 12:07:45
【问题描述】:

我正在尝试从 firebase 读取数据并根据 firebase 中的数据绘制矩形。颜色也会根据从 firebase 检索到的值进行填充。我设法从 firebase 检索数据并将它们附加到数组中。

由于 Firebase 是异步的,我添加了完成处理程序,以便仅在 readMarina() 完成后运行以下代码。我正在尝试通过输入一个矩形框的简单绘图来测试 draw() 方法中的 readMarina{}。一切顺利,直到context?.fill(lot),它给出了一个错误:

“线程 1:EXC_BAD_ACCESS(代码=1,地址=0x7ffe00000058)”

override func draw(_ rect: CGRect)
{
    let context = UIGraphicsGetCurrentContext()
    let lot = CGRect(x: 80,y: 20,width: 30,height: 60)

    readMarina{
        context?.addRect(lot)
        context?.setFillColor(UIColor.red.cgColor)
        context?.fill(lot)
    }
}

func readMarina (_ completion: (() -> Void)?){
    let ref = Database.database().reference()
    let cpDB = ref.child("CarPark")
    cpDB.child("MarinaSq").observe(.value, with: {snapshot in

        let snapshotValue = snapshot.value as! Dictionary<String, AnyObject>

            for (key, value) in snapshotValue 
            {
                if (self.lot.count)<((snapshotValue.count)){
                    let v = value as! String
                    let k = key

                    self.lot.append(k)
                    self.status.append(v)
                }
            }
            completion?()
     }) 
}

我尝试调试,当我将鼠标悬停在“上下文”和“上下文?”上时,它们都显示了一个值。当我将鼠标悬停在“很多”上时,也解析了值。我的代码有什么问题,为什么我的矩形框没有出现在我的视图上?

A picture of the error message

【问题讨论】:

  • 不要尝试使drawRect 异步。在observe的完成处理程序中调用setNeedsDisplay
  • 嗨! :) 您可以在示例代码中向我展示吗?因为我对 swift 很陌生,我不太明白如何在observe 的完成处理程序中调用 setNeedsDisplay。很抱歉给您添麻烦了!
  • 有理由在 draw(_ rect: CGRect) 中调用 readMarina 吗?
  • @Konstantin 我想在这个视图控制器加载时从 firebase 读取数据并将它们附加到数组中

标签: swift firebase swift4 core-graphics xcode10


【解决方案1】:

如draw(_ rect: CGRect)函数描述所说:

当第一次显示视图或发生使视图的可见部分无效的事件时调用此方法。您应该永远不要自己直接调用此方法。要使视图的一部分无效,从而导致该部分被重绘,请改为调用 setNeedsDisplay() 或 setNeedsDisplay(_:) 方法。

draw(_ rect: CGRect) 函数内部的闭包会干扰其执行过程,并且很可能会导致崩溃。要处理这个问题,您应该从 draw(_ rect: CGRect) 内部删除 readMarina 函数调用。这是一个简单的实现:

class MyView: UIView {

    var myColor: UIColor? {
        didSet {
            setNeedsDisplay()
        }
    }

    override func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext(), let color = myColor {
            let lot = CGRect(x: bounds.midX-50, y: bounds.midY-50, width: 100, height: 100)
            context.addRect(lot)
            context.setFillColor(color.cgColor)
            context.fill(lot)
        }
    }

}

我在 Main.storyboard 中的 ViewController 中添加了一个视图,并在身份检查器中将自定义类设置为“MyView”。

 class ViewController: UIViewController {

    @IBOutlet weak var myView: MyView!

    override func viewDidLoad() {
        super.viewDidLoad()
            testFunc {
                self.myView.myColor = UIColor.red
            }
    }

    func testFunc(completion:(() -> Void)?) {
        // test func
        completion?()
    }
}

【讨论】:

  • 在测试函数中,我把从firebase读取的数据放入一个数组中。我按照您所做的操作,但该框仍未出现..
  • 您是否在 Main.storyboard 中将 View 添加到 GridVC 并将其自定义类设置为 MyView?
  • 是的,我已将类设置为 MyView 并将视图添加到 GridVC
  • 能给我你的邮箱吗?我可以把我的整个项目发给你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多