我正试图在Swift应用程序中使用NSView
call getRectsBeingDrawn(_:count:)
,但无法理解如何解压“返回”值--该方法的签名特别神秘。我正在通过count
变量获得预期的矩形数,但我不知道如何访问数组中的矩形。This question解决了同样的问题,并提出了一个解决方案,但它对我不起作用-我无法访问NSRect
结构。
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?
}
发布于 2015-09-12 08:53:38
这就是你想要的:
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
}
您可以创建两个变量rects
和count
,并将引用传递给这两个变量,这样它们就会被信息填充。
调用getRectsBeingDrawn
后,rects
指向count
矩形,您可以使用下标访问这些矩形,就像数组一样。
发布于 2018-06-03 05:29:53
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
复制相似问题