Add dynamically SKLabelNode to MKTVIew

岁酱吖の 提交于 2020-04-30 07:13:29

问题


I am working on metal view that shows the heart rate graph. between every peak of this graph, I need to add a label.

In this process, chart moving from right to left I have an array that dynamically shows the x coordinate of this label for example when one value should show in the screen, this array contain only element and if there are more, it contain more element, this array is named coordinates

I use a for loop to get every element

  for coordinate in coordinates {

  }

I also have another array that dynamically shows the text of the label, and it works same as coordinates. It's named rrIntervalData.

to define this label, I asked another question here: Render text with SKRenderer

here is my code

skScene.size = CGSize(width: CGFloat(viewportSize.x), height: CGFloat(viewportSize.y))

  for coordinate in coordinates {

     var index = 0
     skLabel.text = "\(rrIntervalData[index].value)"
     skLabel.position = CGPoint(x: Int(coordinate), y: 200)
     skLabel.fontSize = 20
     skLabel.fontColor = SKColor.red
     index += 1

  }

skLabel is a SKLabelNode. I also have skScene.addChild(skLabel), that I added in the init of the parent class (I know this is not correct), If I add it here, the app get crashed because skLabel is already added to skScene.

In the current version, the label is created in every point, and it moves with graph (as expected), but when the coordinate of the new label comes to screen, the first label is gone and it shows in the second position, and If I zoom out the graph when it should show more than 20 label in a same time, it always show the label in the last label in the right side.

how can I create a dynamic number of labels that set set here and stick with every coordinate and value?

Your help will be appreciated, It has been more than a week that I couldn't deal with adding this label.

来源:https://stackoverflow.com/questions/61139117/add-dynamically-sklabelnode-to-mktview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!