屏幕左下角的iOS原点反转绘图

时间:2015-08-09 18:57:41

标签: ios swift sprite-kit drawing

当使用SpriteKit绘制交互式线条时,原点从屏幕的左下角开始,而不是从屏幕的左侧开始。结果我的x坐标没问题,但我的y坐标是倒置的。我已经阅读了关于仿射变换但没有理解如何应用它们。这似乎是一个常见的问题,我的代码不应该是错误的。有人能指出我正确的方向吗?非常感谢你。

import UIKit
import SpriteKit

class ViewController: UIViewController {

    @IBOutlet var spriteView: SKView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let scene = GameSprite(size: view.bounds.size)
        spriteView.showsDrawCount = true
        spriteView.showsFPS = true
        spriteView.showsNodeCount = true
        spriteView.ignoresSiblingOrder  = true
        scene.scaleMode = .ResizeFill
        scene.anchorPoint = CGPointMake(0,0) // added since posting
        spriteView.presentScene(scene)
    }
}


import Foundation
import SpriteKit

class GameSprite: SKScene {
    var startPoint = CGPointMake(0,0)
    var endPoint = CGPointMake(0,0)
    var node = SKShapeNode();

    override func didMoveToView(view: SKView) {

    }

    override func touchesBegan(touches: Set<NSObject>, withEvent     event: UIEvent) {
        if let touch = touches.first as? UITouch {
            startPoint = touch.locationInView(view)
        }
        node.removeFromParent()

        addNode()
    }

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        if let touch = touches.first as? UITouch {
            endPoint = touch.locationInView(view)
        }

        modifyNode(endPoint)
    }

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        if let touch = touches.first as? UITouch {
            endPoint = touch.locationInView(view)
        }
        modifyNode(endPoint)
    }

    func addNode() {

        var pathToDraw = CGPathCreateMutable();
        CGPathMoveToPoint(pathToDraw, nil, startPoint.x, startPoint.y);
        CGPathAddLineToPoint(pathToDraw, nil, endPoint.x, endPoint.y);

        node.path = pathToDraw;
        node.strokeColor = SKColor.redColor()
        addChild(node)
    }

    func modifyNode(point: CGPoint) {
        endPoint = point

        var pathToDraw = CGPathCreateMutable();
        CGPathMoveToPoint(pathToDraw, nil, startPoint.x, startPoint.y);
        CGPathAddLineToPoint(pathToDraw, nil, endPoint.x, endPoint.y);

        node.path = pathToDraw;
        node.strokeColor = SKColor.redColor()
        node.removeFromParent()
        addChild(node)
    }
}

0 个答案:

没有答案