问题
I'm trying to use the NSView
call getRectsBeingDrawn(_:count:)
in a Swift app, but can't fathom how to unpack the 'return' values - the method's signature is particularly arcane. I'm getting the expected number of rectangles via the count
variable, but I've no idea how to get access to rectangles in the array. This question addresses the same issue, and proposes a solution, but it's not working for me - I can't get access to NSRect
structs.
func decideWhatToRedraw() {
let r1 = CGRect(x: 0, y: 0, width: 10, height: 20)
let r2 = CGRect(x: 0, y: 100, width: 35, height: 15)
setNeedsDisplayInRect(r1)
setNeedsDisplayInRect(r2)
}
override func drawRect(dirtyRect: NSRect) {
var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = UnsafeMutablePointer<UnsafePointer<NSRect>>.alloc(1)
var count: Int = 0
getRectsBeingDrawn(rects, count: &count)
// count -> 2
// But how to get the rects?
}
回答1:
This is what you want:
var rects = UnsafePointer<NSRect>()
var count = Int()
getRectsBeingDrawn(&rects, count: &count)
for i in 0 ..< count {
let rect = rects[i]
// do things with 'rect' here
}
You make two variables rects
and count
, and pass references to both of them, so they get filled with information.
After calling getRectsBeingDrawn
, rects
points to count
rectangles, which you can access with a subscript, like an array.
回答2:
swift 3.2
var rects: UnsafePointer<NSRect>?
var count = Int()
getRectsBeingDrawn(&rects, count: &count)
for i in 0 ..< count {
let rect = NSIntersectionRect(bounds, rects![i]);
NSRectFillUsingOperation(rect, NSCompositeSourceOver)
}
来源:https://stackoverflow.com/questions/32536855/using-getrectsbeingdrawn-with-swift