Trying to create a CGMutablePath() as such:
let path = CGMutablePath()
CGPathMoveToPoint(path, nil, 30, 0)
but the compiler keeps on giving me the following error: 'nil' not compatible with expected argument type 'UnsafePointer<CGAffineTransform>'. What am I doing wrong? I can't seem to find anything else online.
(Yes I've tried converting the Ints to CGFloats but it doesn't seem to make a difference.)
Try this:
let path = CGMutablePath()
path.move(to: CGPoint(x: 30, y: 0))
CGPath APIs are now imported as instance methods in Swift 3.
You can check them with Command-clicking on CGMutablePath.
Or see the latest reference of CGMutablePath.
I was getting this error when trying to draw inside a CGRect
This solved it for me:
let framePath : CGMutablePath = CGMutablePath()
framePath.addRect(frameRect)
I hope this saves you countless hours trying to solve this. :)
Related
I want to create a shape at a random position on a screen in a universal app.
If I make a GKRandomDistribution the shape.position.x or shape.position.y gives me an error saying that it cannot convert an Int into a CGPoint.
If I knew the size of the screen this would be easy, but since I want to make this universal all I have to go on is self.frame and I can't find a way of saying something like GKRandomDistribution(lowestValue: <min value of screen>, highestValue: <max value of screen>) for my positions.
I'd love some help with this!
I'm assuming you'll be calling this within one of your SKScene subclasses. If I understand your question correctly, this should do the trick
func randomPosition() -> CGPoint {
let x = CGFloat.random(in: 0...frame.maxX)
let y = CGFloat.random(in: 0...frame.maxY)
return CGPoint(x: x, y: y)
}
Trying to create a CGMutablePath() as such:
let path = CGMutablePath()
CGPathMoveToPoint(path, nil, 30, 0)
but the compiler keeps on giving me the following error: 'nil' not compatible with expected argument type 'UnsafePointer<CGAffineTransform>'. What am I doing wrong? I can't seem to find anything else online.
(Yes I've tried converting the Ints to CGFloats but it doesn't seem to make a difference.)
Try this:
let path = CGMutablePath()
path.move(to: CGPoint(x: 30, y: 0))
CGPath APIs are now imported as instance methods in Swift 3.
You can check them with Command-clicking on CGMutablePath.
Or see the latest reference of CGMutablePath.
I was getting this error when trying to draw inside a CGRect
This solved it for me:
let framePath : CGMutablePath = CGMutablePath()
framePath.addRect(frameRect)
I hope this saves you countless hours trying to solve this. :)
Trying to create a CGMutablePath() as such:
let path = CGMutablePath()
CGPathMoveToPoint(path, nil, 30, 0)
but the compiler keeps on giving me the following error: 'nil' not compatible with expected argument type 'UnsafePointer<CGAffineTransform>'. What am I doing wrong? I can't seem to find anything else online.
(Yes I've tried converting the Ints to CGFloats but it doesn't seem to make a difference.)
Try this:
let path = CGMutablePath()
path.move(to: CGPoint(x: 30, y: 0))
CGPath APIs are now imported as instance methods in Swift 3.
You can check them with Command-clicking on CGMutablePath.
Or see the latest reference of CGMutablePath.
I was getting this error when trying to draw inside a CGRect
This solved it for me:
let framePath : CGMutablePath = CGMutablePath()
framePath.addRect(frameRect)
I hope this saves you countless hours trying to solve this. :)
After converting from Swift 2 to Swift 3 (even after converting edit-> convert -> to current swift syntax) I am getting lots of errors. Especially:
I am shown total 90 errors for my project which was working fine in Swift 2 before i downloaded this beta Xcode 8.0 and converted to Swift 3
Is this a conversion mistake I am making?
Most of them are easy fixes, simply by tapping the red button, and having Xcode fix it for you! Others include:
CGRect
Swift 2:
let frame = CGRectMake(0, 0, 20, 20)
Swift 3:
let frame = CGRect(x: 0, y: 0, width: 20, height: 20)
CGPoint
Swift 2:
let point = CGPointMake(0, 0)
Swift 3:
let point = CGPoint(x: 0, y: 0)
CGSize
Swift 2:
let size = CGSizeMake(20, 20)
Swift 3:
let size = CGSize(width: 20, height: 20)
CGRectGetMidX
Swift 2:
CGRectGetMidX(view)
Swift 3:
view.midX
CGRectGetMidY
Swift 2:
CGRectGetMidY(view)
Swift 3:
view.midY
UIColor
Swift 2:
let color = UIColor.redColor()
Swift 3:
let color = UIColor.red
"NS"
Swift 2:
NSTimer
NSData
NSError
Swift 3:
Timer
Data
Error
UserDefaults
Swift 2:
NSUserDefaults.standardUserDefaults().//something
Swift 3:
UserDefaults.standard.//something
And always remember to use the helpful "Fix all in Scope" function which can be found at Editor -> Fix all in Scope
I was converting a project and Xcode was not helping me with any fixes so I resorted to a couple of regex search-and-replaces:-
CGPointMake\((.*),[ ]*([^\)]+)\)
CGPoint(x:$1, y:$2)
CGSizeMake\((.*),[ ]*([^\)]+)\)
CGSize(width:$1, height:$2)
Note they are not aware of nested parentheses , but probably good enough for 90% of cases.
I'm getting an image's Size and used the below code. It was working perfectly fine with Xcode 6.0.1. After updating to Xcode 6.1, I'm getting Error Like :
'UIImage?' does not have a member named 'size'
var image = UIImage(named: imageName)
let sizeOfImage = image.size
Is it bug with my code or Apple ? Please help me in here. TIA.
That initializer is now a failable initializer, and as such it returns an optional UIImage.
To quickly fix your bug just unwrap the image:
let sizeOfImage = image?.size
but I presume you will reference the image variable more than just once in your code, in that case I suggest using optional binding:
if let image = image {
let sizeOfImage = image.size
/// ...
/// Use the image
}
In addition to #Antonio's answer you can use nil coalescing operator(??) to get a default size in case image initializer is failed. Here is how:
let size = image?.size ?? CGSizeZero
Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. In your case: image.size!
you can use like this:
let image = UIImage(named: "photo1.png")
let sizeOfImage = image?.size
imageView.image = image
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: sizeOfImage!)