【问题标题】:Swift 3/SpriteKit - Draw independent shapes with fingerSwift 3/SpriteKit - 用手指绘制独立的形状
【发布时间】:2017-05-09 16:42:13
【问题描述】:

我是一名初学者,正在学习使用 Swift 绘制形状的教程 here。

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() 的每个新实例创建一个点数组?

感谢您的帮助。

【问题讨论】:

    标签: ios swift sprite-kit draw uibezierpath


    【解决方案1】:

    有点像

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

    【讨论】:

    • 谢谢!为了使它工作,除了您的代码之外,我还必须将 activeSlicePoints.removeAll(keepingCapacity: true) 添加到 touchesBegan()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多