Using getRectsBeingDrawn: with Swift

安稳与你 提交于 2019-12-13 03:37:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!