Swift numerics and CGFloat (CGPoint, CGRect, etc.)
I wrote a library that handles operator overloading to allow interaction between Int, CGFloat and Double.
https://github.com/seivan/ScalarArithmetic
As of Beta 5, here's a list of things that you currently can't do with vanilla Swift.
https://github.com/seivan/ScalarArithmetic#sample
I suggest running the test suite with and without ScalarArithmetic just to see what's going on.
Specifying a CGRect of Int on a function header
CGRect structure can contain be of Int, Double or CGFloat.
No, it can't.
Though it's possible to initialize a CGRect structure with Int, Double or CGFloat values, internally all struct members are based on the CGFloat
type.
Please see the documentation
What libraries do we need to include to use CGPoint and arc4random()?
CGPoint
is defined in CoreGraphics
, but will also be imported by default when importing a UI library like UIKit
or SwiftUI
.
arc4random
in part of Darwin
(the kernel) but, again, will be imported automatically when you import an Apple framework that depends on it, like Foundation
or even CoreGraphics
as well.
import CoreGraphics
// ... your code here
Split the CGPoint numbers in two CGFloat
CGPoint
has properties x
and y
:
person1 = UIImageView(frame: CGRectMake(point.x, point.y, 25, 25))
Alternatively create the CGRect
directly from your point as the origin
and a given size
:
person1 = UIImageView(frame: CGRect(origin: point, size: CGSize(width: 25, height: 25)))
Is there any shorthand way to write CGPoint, CGRect, etc?
As already suggested in the comments you can create your own extension.
Extension
You can add an initializer that accepts 2 CGFloat(s)
and doesn't require external param names
extension CGPoint {
init(_ x: CGFloat, _ y: CGFloat) {
self.x = x
self.y = y
}
}
let point = CGPoint(1, 2)
Infix Operator
You can also define your infix operator
infix operator ° {}
func °(x: CGFloat, y: CGFloat) -> CGPoint {
return CGPoint(x: x, y: y)
}
let point = 1.1 ° 1.1
How to convert CGRect to CGPoint iOS Swift
You have to pass a CGPoint
instead of CGFloat
as below,
// set x, y as per your requirements.
let point = CGPoint(x: imgPin.frame.minX, y: imgPin.frame.minY)
let cgPoint = imgPin.convert(point, to: self.view)
OR
You can pass the CGRect
as it is and get the point as origin
,
let cgPoint = v.convert(imgPin.frame, to: self.view).origin
Related Topics
Xcode 4: Build Failed, No Issues
Add Left/Right Horizontal Padding to Uilabel
Automatic Otp Verification in iOS
Wait for Asynchronous Operation to Complete in Swift
Avaudioplayer Throws Breakpoint in Debug Mode
How to Set Imageview in Circle Like Imagecontacts in Swift Correctly
Upload Image to the PHP Server from iOS
Uploading Archive Error: "Missing iOS Distribution Signing Identity for ..."
Table Header View Height Is Wrong When Using Auto Layout, Ib, and Font Sizes
iOS Designated Initializers:Using Ns_Designated_Initializer
Disable Swipe Back Gesture in Swift
Is iOS Carplay API Public? How to Integrate Carplay
How to Implement Two Inits with Same Content Without Code Duplication in Swift