gpt4 book ai didi

ios - 如何修复 SpriteKit 风格的分段 Controller 元素的模糊性

转载 作者:行者123 更新时间:2023-11-30 11:46:30 25 4
gpt4 key购买 nike

我正在 Swift for iOS 中使用 SpriteKit 构建游戏。我正在实现一个带有 UI 风格元素的设置面板。我想添加一个 UISegmentedControl 元素,但我无法找到一种快速、简单的方法将 UIKit 元素添加到我的场景中,不需要我创建 2-3 个新文件并添加 100 多行代码。因此,我决定使用 SKCropNode 和 SKShapeNodes 实现我自己版本的 UISegmentedControl 元素。我已经成功地做到了这一点,没有太多麻烦,但是当我模拟应用程序时,我的 SKLabelNodes 中的文本显得模糊。

问题是当应用程序在横向模式下模拟时,文本显得模糊。在纵向模式下,文字看起来相当清晰,但在横向模式下,文字有点模糊。

这是类文件

import SpriteKit

class SegController {
var mainNode: SKShapeNode
var itemShapes = [SKShapeNode]()


var segIndex = 0

init(withSize size: CGSize, withItems items: [String]) {
let cropNode = SKCropNode()
var maskSize = size
maskSize.width -= 3.5; maskSize.height -= 4
let mask = SKShapeNode(rectOf: maskSize, cornerRadius: 5)
mask.fillColor = UIColor.white
cropNode.maskNode = mask
mainNode = SKShapeNode(rectOf: maskSize, cornerRadius: 5)
mainNode.addChild(cropNode)
mainNode.lineWidth = 2.5
mainNode.strokeColor = UIColor.black

var loopIndex = 0
for _ in items {
let itemShapeWidth = size.width / CGFloat(items.count)
let itemShape = SKShapeNode(rectOf: CGSize(width: itemShapeWidth, height: size.height))
itemShape.lineWidth = 0.5
itemShape.strokeColor = UIColor.black

var xPos = itemShapeWidth * (CGFloat(loopIndex))
xPos -= size.width / 2
xPos += itemShapeWidth / 2
itemShape.position = CGPoint(x: xPos, y: 0)

cropNode.addChild(itemShape)
itemShapes.append(itemShape)

let label = SKLabelNode(fontNamed: "Arial")
let fontSize = (8 + size.height / 6)
label.fontSize = fontSize
let yPos = -fontSize / 2.5
label.position = CGPoint(x: 0, y: yPos)
label.name = "label"

label.text = items[loopIndex]
itemShape.addChild(label)

if loopIndex == 0 {
itemShape.fillColor = UIColor.black
label.fontColor = UIColor.white
} else {
itemShape.fillColor = UIColor.white
label.fontColor = UIColor.black
}

loopIndex += 1
}

}

func handleTap(withTouch touch: UITouch) {
let location = touch.location(in: mainNode)
var loopIndex = 0
for itemShape in itemShapes {
if itemShape.contains(location) && segIndex != loopIndex{
itemShape.fillColor = UIColor.black
let newLabel = getLabel(ofItem: itemShape)!
newLabel.fontColor = UIColor.white
itemShapes[segIndex].fillColor = UIColor.white
let oldLabel = getLabel(ofItem: itemShapes[segIndex])
oldLabel?.fontColor = UIColor.black
segIndex = loopIndex
return
}
loopIndex += 1
}

}

func getLabel(ofItem item: SKShapeNode) -> SKLabelNode? {
for child in item.children {
if child.name == "label" {
return child as? SKLabelNode
}
}
return nil
}
}

下面是将类的实例添加到 GameScene.swift 文件的必要代码:

let segControlOne = SegController(withSize: CGSize(width: 400 , height: 60), withItems: ["One","Two"])

self.addChild(segControlOne.mainNode)

最佳答案

我建议在创建 SKLabelNode 时减少大量计算。这几乎是预期的行为,因为 SKLabelNode 针对精度进行了性能优化,因此您可以尝试强制计算始终使用整数。使用horizo​​ntalAlignmentMode 将其居中并舍入为整数。我通常使用 UiKit 来处理我需要的所有静态图形,使用 SpriteKit 来处理游戏元素。

关于ios - 如何修复 SpriteKit 风格的分段 Controller 元素的模糊性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48775644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com