Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用函数调用正确更新CGContext?_Ios_Swift_Uiview_Cgcontext - Fatal编程技术网

Ios 如何使用函数调用正确更新CGContext?

Ios 如何使用函数调用正确更新CGContext?,ios,swift,uiview,cgcontext,Ios,Swift,Uiview,Cgcontext,我正在制作一个小型射击游戏,在这个游戏中,一艘小船探索一个8x8级别的房间,里面充满了敌人等等。我目前正在尝试实现一个地图,显示玩家已经去了哪里。我最初的实现很简单——我制作了一个自定义UIView,将它附加到我的故事板上,并且只想在地图上绘制任何访问过的新房间。我选择使用CGContext来实现这一点,我的第一次尝试如下所示 有了这段代码,第一个updateMap调用就完美地工作了——矩形在玩家地图上以正确的颜色和位置绘制。然而,尽管CGRect更新正确,任何后续调用似乎都不会产生任何效果。如

我正在制作一个小型射击游戏,在这个游戏中,一艘小船探索一个8x8级别的房间,里面充满了敌人等等。我目前正在尝试实现一个地图,显示玩家已经去了哪里。我最初的实现很简单——我制作了一个自定义UIView,将它附加到我的故事板上,并且只想在地图上绘制任何访问过的新房间。我选择使用CGContext来实现这一点,我的第一次尝试如下所示

有了这段代码,第一个updateMap调用就完美地工作了——矩形在玩家地图上以正确的颜色和位置绘制。然而,尽管CGRect更新正确,任何后续调用似乎都不会产生任何效果。如果我取消对PopContext的注释(或删除PushContext),它将触发“Context not found”抛出。虽然下面显示的代码不会打印“未找到上下文”,但也不会更新映射

诚然,我对CGContext的理解相当平淡,所以我认为我在这里只是犯了一个概念上的错误。话虽如此,我到底搞砸了什么

class MapView: UIView {


var x: Int!
var y: Int!
var drect: CGRect!

override func draw( _ frame: CGRect) {
    
    super.draw(frame)
    
    if let context: CGContext = UIGraphicsGetCurrentContext(){
        //UIGraphicsPopContext()
        context.setFillColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.0);   //this is the transparent color
        context.setStrokeColor(red: 0.0, green: 0.0, blue: 100.0, alpha: 1.0);
        context.fill(drect);
        context.stroke(drect);    //this will draw the border
        context.addRect(drect)
        UIGraphicsPushContext(context)
    } else {
        print("Context not found")
        return
    }



}

public func updateMap(scene: GameScene){
    x = scene.current_xcoords
    y = scene.current_ycoords
    let w = self.frame.width
    let h = self.frame.height
    drect = CGRect(x: (w/50) + CGFloat(x)*w/8, y: (h/100) + CGFloat(y)*h/8, width: (h / 11), height: (h / 11))
    draw(self.frame)
}

}

您不应该直接调用
draw()
方法。。。您可以调用
setNeedsDisplay()
layoutifneed()

依照

首次显示视图或事件时调用此方法 使视图的可见部分无效的事件。你不应该 自己直接调用这个方法。要使你的观点无效, 并因此导致该部分被重新绘制,调用setNeedsDisplay() 或者改为setNeedsDisplay(:)方法

所以你的代码变成了

public func updateMap(scene: GameScene){
    x = scene.current_xcoords
    y = scene.current_ycoords
    let w = self.frame.width
    let h = self.frame.height
    drect = CGRect(x: (w/50) + CGFloat(x)*w/8, y: (h/100) + CGFloat(y)*h/8, width: (h / 11), height: (h / 11))
    setNeedsDisplay()
}

就这样!谢谢,真不敢相信我在看文件的时候漏掉了这个细节。