缺少UIEvent类型?

时间:2019-04-15 14:14:43

标签: swift sprite-kit

我正在将XCode 10.2与SpriteKit和swift 5一起使用来创建Mac应用程序,我一生都找不到处理节点上的点击事件的有效示例。

我可以找到的最新信息建议使用以下内容:

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

但是XCode 错误,说

Use of undeclared type 'UIEvent'
Use of undeclared type 'UITouch'

我不确定如何进行处理,因为使用上述代码我发现建议的所有信息。到目前为止,其他所有工作都正常。这是我的GameScene类:

import SpriteKit

class GameScene: SKScene {

    var circle: SKShapeNode = SKShapeNode();
    var label: SKLabelNode = SKLabelNode();
    var ground: SKShapeNode = SKShapeNode();

    /**
     * Did Move Callback
     */
    override func didMove(to view: SKView) {
        initGround(view: view);
        initLabel(view: view);

        let recognizer = NSClickGestureRecognizer(target: self, action: #selector(tap));
        view.addGestureRecognizer(recognizer);
    }

    /**
     * Click Handler
     */
    @objc func tap(recognizer: NSGestureRecognizer) {
        let viewLocation = recognizer.location(in: view);
        let sceneLocation = convertPoint(fromView: viewLocation);

        addCircle(view: view!, position: sceneLocation);
    }

    // Not Working
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


    }

    /**
     * Init Ground
     */
    func initGround(view: SKView) {

        let path = CGMutablePath();
        path.move(to: CGPoint(x: 0, y: 30));
        path.addLine(to: CGPoint(x: view.frame.width, y: 30));
        path.addLine(to: CGPoint(x: view.frame.width, y: view.frame.height));
        path.addLine(to: CGPoint(x: 0, y: view.frame.height));
        path.addLine(to: CGPoint(x: 0, y: 30));

        ground.path = path;
        ground.lineWidth = 5;
        ground.strokeColor = SKColor.brown;

        let borderBody = SKPhysicsBody(edgeLoopFrom: path);
        borderBody.friction = 5;
        self.physicsBody = borderBody;

        addChild(ground);
    }

    /**
     * Add Label
     */
    func initLabel(view: SKView) {

        label = SKLabelNode(text: "No Gravity");
        label.name = "no gravity button";
        label.position = CGPoint(x: label.frame.width / 2, y: 5);
        label.fontSize = 12;
        label.fontColor = SKColor.yellow;
        label.fontName = "Avenir";
        label.isUserInteractionEnabled = true;

        addChild(label);
    }

    /**
     * Add Circle
     */
    func addCircle(view: SKView, position: CGPoint) {
        let size = Int.random(in: 1 ..< 50);
        let color = getRandomColor();

        circle = SKShapeNode(circleOfRadius: CGFloat(size));

        // Set Position
        circle.position = position;

        // Set Physics
        circle.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(size));
        circle.physicsBody?.affectedByGravity = false;
        circle.physicsBody?.usesPreciseCollisionDetection = true;
        circle.physicsBody?.restitution = 1; //0.8
        circle.physicsBody?.linearDamping = 0;
        circle.physicsBody?.friction = 0; //0.2
        circle.physicsBody?.isDynamic = true;
        circle.physicsBody?.mass = CGFloat(size);
        circle.physicsBody?.allowsRotation = true;
        circle.physicsBody?.velocity = CGVector(dx: 0, dy: -1000);

        circle.strokeColor = color;
        circle.glowWidth = 1.0;
        circle.fillColor = color;

        addChild(circle)
    }

    func getRandomColor() -> SKColor {
        return SKColor(red: .random(in: 0...1),
                       green: .random(in: 0...1),
                       blue: .random(in: 0...1),
                       alpha: 1.0)
    }
}

有人在使用Swift 5和处理SpriteKit中的单击/触摸方面有经验吗?还是也许XCode为什么说UIEvent和UITouch不是真实的东西?

谢谢!

2 个答案:

答案 0 :(得分:0)

在Mac上,您将实现

autoComplete

在每种情况下,您都会致电

-(void)mouseDown:(NSEvent *)theEvent {
}

- (void)mouseUp:(NSEvent *)theEvent {
}

- (void)mouseExited:(NSEvent *)theEvent {
}

了解点击在场景中发生的位置。

答案 1 :(得分:0)

用户Martin R指出UIEvent和UITouch类仅包含在iOS项目中。我正在创建一个Mac应用程序,但我没有意识到我的GameScene类中已经有单击处理程序:

override func didMove(to view: SKView) {
    let recognizer = NSClickGestureRecognizer(target: self, action: #selector(click));
    view.addGestureRecognizer(recognizer);
}

这将为我已经定义的点击处理程序注册一个回调:

/**
 * Click Handler
 */
@objc func tap(recognizer: NSGestureRecognizer) {
    let viewLocation = recognizer.location(in: view);
    let sceneLocation = convertPoint(fromView: viewLocation);
    let touchedNode = self.atPoint(sceneLocation);

    if let name = touchedNode.name {
        if name == "no gravity button" {
            print("Touched")
        }
    }

    addCircle(view: view!, position: sceneLocation);
}

我能够获得被单击的节点名称,并按照我的原意对其进行操作。