ios Swift 中的 CGPointMake

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24070408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:33:43  来源:igfitidea点击:

CGPointMake in Swift

iosobjective-cswiftsprite-kitcgpoint

提问by RaffAl

How to use CGPointMakein Swift? Is there an equivalent for it? I am getting an error:

如何使用CGPointMakeSwift?它有等价物吗?我收到一个错误:

Use of unresolved identifier 'CGPointMake'

使用未解析的标识符“CGPointMake”

Basically, I am trying to assign a position to a Sprite Kit node and cannot figure out how to do it in Swift.

基本上,我正在尝试为 Sprite Kit 节点分配一个位置,但无法弄清楚如何在 Swift 中执行此操作。

class PlayerSpaceship: Spaceship {

    func launchMissile() {

        var missile = Missile.playerMissile()

        // This line gives above mentioned error.    
        missile.position = CGPointMake(0.0, 0.0) 
    }
}

回答by Joseph Mark

Use CGPoint(x: Float, y: Float)

CGPoint(x: Float, y: Float)

回答by Connor

You call it a little differently, without the make.

你叫它有点不同,没有品牌。

CGPoint(x: 10, y: 20)

回答by mike663

Xcode 6.3.1 shows 4 different Swift initializers for CGPoint. They are:

Xcode 6.3.1 为 CGPoint 展示了 4 种不同的 Swift 初始值设定项。他们是:

CGPoint()
CGPoint(x: CGFloat, y: CGFloat)
CGPoint(x: Double, y: Double)
CGPoint(x: Int, y: Int)

回答by Frank Schaefer

In the code it should looks like this (Xcode 6.1):

在代码中它应该是这样的(Xcode 6.1):

let point: CGPoint = CGPoint(x:10,y:10)

回答by Frank Schaefer

Or also, you can use CGFloattype for CGPoint

或者,您也可以将CGFloat类型用于 CGPoint

 CGPoint(x: CGFloat, y: CGFloat)