I'm trying to call CGRectDivide and can't figure out the syntax in Swift. The language guide doesn't seem to be any help.
Here's the template:
CGRectDivide(rect: CGRect, slice: CMutablePointer<CGRect>, remainder: CMutablePointer<CGRect>, amount: CGFloat, edge: CGRectEdge)
My code:
var r = UIScreen.mainScreen().bounds
var mySlice: CGRect
var myRemainder: CGRect
CGRectDivide(rect: CGRect, slice: mySlice<CGRect>, remainder: myRemainder<CGRect>, amount: 44.0, edge: CGRectEdge.MaxYEdge)
That gives me an error: "Cannot specialize a non-generic definition"
I'm stumped. So much for Swift being a very readable language.
If you Command-click on a CGRect symbol in your code, you'll be taken to the Swift declaration of struct CGRect. In its first extension you'll find a number of useful methods, including the following:
Swift 2:
func divide(atDistance: CGFloat, fromEdge: CGRectEdge) -> (slice: CGRect, remainder: CGRect)
Swift 3:
func divide(_ atDistance: CGFloat, fromEdge: CGRectEdge) -> (slice: CGRect, remainder: CGRect)
Which you could use like this:
import UIKit
let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
let (slice, remainder) = rect.divide(10.0, fromEdge: .MinXEdge) // Swift 3: .minXEdge
print("rect: \(rect)") // prints "rect: (0.0,0.0,100.0,100.0)"
print("slice: \(slice)") // prints "slice: (0.0,0.0,10.0,100.0)"
print("remainder: \(remainder)") // prints "remainder: (10.0,0.0,90.0,100.0)"
Just sample, how you can try to do it:
let rect = CGRectMake(0, 0, 100, 100)
var sliceRect = CGRectZero
var remainderRect = CGRectZero
CGRectDivide (rect, &sliceRect, &remainderRect, 30.0, "CGRectEdge");
print(sliceRect)
print(remainderRect)
"CGRectEdge" should be replaced by:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With