CGRectMake, CGPointMake, CGSizeMake, CGRectZero, C

2019-01-01 04:40发布

After converting code to latest swift 3.0 I am shown this error.

enter image description hereenter image description here

Also tell me solution for CGSize = CGSizeMake(0,0)

static var frameAtStartOfPan: CGRect = CGRectZero
static var startPointOfPan: CGPoint = CGPointZero

Which is also unavailable.

9条回答
春风洒进眼中
2楼-- · 2019-01-01 05:26

If you want to use them as in swift 2, you can use these funcs:

For CGRectMake:

func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
    return CGRect(x: x, y: y, width: width, height: height)
}

For CGPointMake:

func CGPointMake(_ x: CGFloat, _ y: CGFloat) -> CGPoint {
    return CGPoint(x: x, y: y)
}

For CGSizeMake:

func CGSizeMake(_ width: CGFloat, _ height: CGFloat) -> CGSize {
    return CGSize(width: width, height: height)
}

Just put them outside any class and it should work. (Works for me at least)

查看更多
与风俱净
3楼-- · 2019-01-01 05:29

You can use this with replacement of CGRectZero

CGRect.zero
梦寄多情
4楼-- · 2019-01-01 05:30

The structures as well as all other symbols in Core Graphics and other APIs have become cleaner and also include parameter names. https://developer.apple.com/reference/coregraphics#symbols

CGRect(x: Int, y: Int, width: Int, height: Int)
CGVector(dx: Int, dy: Int)
CGPoint(x: Double, y: Double)
CGSize(width: Int, height: Int)

CGPoint Example:

let newPoint = stackMid.convert(CGPoint(x: -10, y: 10), to: self)

CGVector Example:

self.physicsWorld.gravity = CGVector(dx: 0, dy: gravity)

CGSize Example:

hero.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 16, height: 18))

CGRect Example:

let back = SKShapeNode(rect: CGRect(x: 0-120, y: 1024-200-30, width: 240, height: 140), cornerRadius: 20)

Apple Blog

Swift syntax and API renaming changes in Swift 3 make the language feel more natural, and provide an even more Swift-y experience when calling Cocoa frameworks. Popular frameworks Core Graphics and Grand Central Dispatch have new, much cleaner interfaces in Swift.

查看更多
不再属于我。
5楼-- · 2019-01-01 05:34

In Swift 4 it works like below

collectionView.setContentOffset(CGPoint.zero, animated: true)
看淡一切
6楼-- · 2019-01-01 05:35

Quick fix for CGRectMake , CGPointMake, CGSizeMake in Swift3 & iOS10

Add these extensions :

extension CGRect{
    init(_ x:CGFloat,_ y:CGFloat,_ width:CGFloat,_ height:CGFloat) {
        self.init(x:x,y:y,width:width,height:height)
    }

}
extension CGSize{
    init(_ width:CGFloat,_ height:CGFloat) {
        self.init(width:width,height:height)
    }
}
extension CGPoint{
    init(_ x:CGFloat,_ y:CGFloat) {
        self.init(x:x,y:y)
    }
}

Then go to "Find and Replace in Workspace" Find CGRectMake , CGPointMake, CGSizeMake and Replace them with CGRect , CGPoint, CGSize

These steps might save all the time as Xcode right now doesn't give us quick conversion from Swift 2+ to Swift 3

查看更多
余欢
7楼-- · 2019-01-01 05:39

In Swift 3, you can simply use CGPoint.zero or CGRect.zero in place of CGRectZero or CGPointZero.

However, in Swift 4, CGRect.zero and 'CGPoint.zero' will work

登录 后发表回答