在Swift会话中,苹果2015年WWDC协议面向编程的代码行是做什么的?
var draw: (CGContext)->() = { _ in () }使用这一行代码的SWIFT2.1版本的演示操场和文件可以在这里找到:https://github.com/alskipp/Swift-Diagram-Playgrounds/blob/master/Crustacean.playground/Sources/CoreGraphicsDiagramView.swift
我试图了解如何为所有的Drawable调用CGContextStrokePath(上下文)。
发布于 2015-12-04 17:46:21
它是一个带有闭包的属性(函数,或者更好:一个代码块,在本例中使用CGContext作为参数)。它什么都不做。它忽略了CGContext (这是_ in部分)。
在后面的示例中,有这样的函数:
public func showCoreGraphicsDiagram(title: String, draw: (CGContext)->()) {
let diagramView = CoreGraphicsDiagramView(frame: drawingArea)
diagramView.draw = draw
diagramView.setNeedsDisplay()
XCPlaygroundPage.currentPage.liveView = diagramView
}如果可以提供另一个闭包(CGContext) -> (),则将这个新闭包分配给draw属性。
在drawRect函数中,它被调用:draw(context)。
因此,基本上您可以提供一个代码块来绘制一些东西,例如
showCoreGraphicsDiagram("Diagram Title", draw: { context in
// draw something using 'context'
})或者更短的“尾随闭包语法”:
showCoreGraphicsDiagram("Diagram Title") { context in
// draw something using 'context'
}https://stackoverflow.com/questions/34094048
复制相似问题