gpt4 book ai didi

ios - Swift 3/SpriteKit - 用手指绘制独立的形状

转载 作者:行者123 更新时间:2023-11-28 08:10:38 25 4
gpt4 key购买 nike

我是遵循教程的初学者 here用于在 Swift 中绘制形状。

import SpriteKit

class GameScene: SKScene {

var activeSlice: SKShapeNode!
var activeSlicePoints = [CGPoint]()

override func didMove(to view: SKView) {
createSlices()
}

func createSlices() {
activeSlice = SKShapeNode()
activeSlice.strokeColor = UIColor(red: 1, green: 0.9, blue: 0, alpha: 1)
activeSlice.lineWidth = 9
addChild(activeSlice)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if let touch = touches.first {
let location = touch.location(in: self)
activeSlicePoints.append(location)
redrawActiveSlice()
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
activeSlicePoints.append(location)
redrawActiveSlice()

}

override func touchesEnded(_ touches: Set<UITouch>?, with event: UIEvent?) {
}

func redrawActiveSlice() {
let path = UIBezierPath()
path.move(to: activeSlicePoints[0])
for i in 1 ..< activeSlicePoints.count {
path.addLine(to: activeSlicePoints[i])
}
activeSlice.path = path.cgPath
}
}

使用此代码,当您松开手指然后再次触摸进行绘制时,您会得到一条连接两个形状的线。我想画独立的形状。或许通过修改代码,以便对于 touchesEnded() 的每个实例,表示形状的点数组存储在一个多维数组中,并通过为 touchesBegan() 的每个新实例创建一个点数组。 ?

感谢您的帮助。

最佳答案

有点像

override func touchesEnded(_ touches: Set<UITouch>?, with event: UIEvent?) {

createSlices()
}

关于ios - Swift 3/SpriteKit - 用手指绘制独立的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43875465/

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