【发布时间】:2016-10-04 14:47:32
【问题描述】:
我正在编写一个由 CGPoints 组成的应用程序构建元素。我有 2 个按钮:makeRectangle 和 makeTriangle。对于构建/绘图阶段,我在drawRect 中使用了三种rectangle 方法和三种triangle 方法。
我在drawRect 中的代码卡住了。在if-else-statement 中,每次按下按钮时,每个方法都会为前一个元素交换建筑/绘图方案。
如果我已经建立了矩形然后我点击makeTriangle按钮,我得到了新的三角形,但是我的矩形变成了一个没有连接点的三角形。
有解决方法还是我不应该使用drawRect 方法?
这是关于 drawRect 主题的 SO 帖子:To drawRect or not to drawRect
元素声明:
enum Element {
case point1(point: CGPoint)
case point2(point: CGPoint)
case point3(point: CGPoint)
case point4(point: CGPoint)
func coord() -> [CGPoint] {
switch self {
case .point1(let point): return [point]
case .point2(let point): return [point]
case .point3(let point): return [point]
case .point4(let point): return [point]
}
}
func buildQuadPath(path: CGMutablePath) {
switch self {
case .point1(let point): CGPathMoveToPoint(path, nil, point.x, point.y)
case .point2(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
case .point3(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
case .point4(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
CGPathCloseSubpath(path)
}
}
func buildTriPath(path: CGMutablePath) {
switch self {
case .point1(let point): CGPathMoveToPoint(path, nil, point.x, point.y)
case .point2(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
case .point3(let point): CGPathAddLineToPoint(path, nil, point.x, point.y)
default:
CGPathCloseSubpath(path)
}
}
}
三角形和矩形的构建和绘制方法:
func buildTriPath() -> CGMutablePath {
let path = CGPathCreateMutable()
_ = array.map { $0.buildTriPath(path) }
return path
}
func drawTriPath() {
let path = buildTriPath()
GraphicsState {
CGContextAddPath(self.currentContext, path)
CGContextStrokePath(self.currentContext)
}
}
func drawTriFill() {
let fill = buildTriPath()
GraphicsState {
CGContextAddPath(self.currentContext, fill)
CGContextFillPath(self.currentContext)
}
}
/////////////////////////////////////// ////////
func buildQuadPath() -> CGMutablePath {
let path = CGPathCreateMutable()
_ = array.map { $0.buildQuadPath(path) }
return path
}
func drawQuadPath() {
let path = buildQuadPath()
GraphicsState {
CGContextAddPath(self.currentContext, path)
CGContextStrokePath(self.currentContext)
}
}
func drawQuadFill() {
let fill = buildQuadPath()
GraphicsState {
CGContextAddPath(self.currentContext, fill)
CGContextFillPath(self.currentContext)
}
}
两个变量帮助判断按钮是否被按下:
var squareB: Int = 0
var triangleB: Int = 0
@IBAction func makeTriangle(sender: AnyObject?) {
....................
....................
triangleB += 1
squareB = 0
}
@IBAction func makeRectangle(sender: AnyObject?) {
....................
....................
triangleB = 0
squareB += 1
}
drawRect方法:
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
drawBG()
GraphicsState { self.drawMyPoints() }
if squareB >= 1 && triangleB == 0 {
buildQuadPath()
drawQuadPath()
drawQuadFill()
needsDisplay = true
}
else if triangleB >= 1 && squareB == 0 {
buildTriPath()
drawTriPath()
drawTriFill()
needsDisplay = true
}
drawBorder()
}
...最后是Context.swift 文件:
import Cocoa
import CoreGraphics
extension NSView {
var currentContext : CGContext? {
get {
let unsafeContextPointer = NSGraphicsContext.currentContext()?.graphicsPort
if let contextPointer = unsafeContextPointer {
let opaquePointer = COpaquePointer(contextPointer)
let context: CGContextRef = Unmanaged.fromOpaque(opaquePointer).takeUnretainedValue()
return context }
else { return nil }
}
}
func GraphicsState(drawStuff: () -> Void) {
CGContextSaveGState(currentContext)
drawStuff()
CGContextRestoreGState(currentContext)
}
}
//the end of code
【问题讨论】:
-
您的陈述与提供的代码相结合是没有意义的。如果我猜对了,你的默认状态是 0。如果你按下一个按钮(我猜你启用了某种点击监听器),状态会变为 1。所以应该只绘制两个选项中的一个。检查您的代码,如果您的默认状态已正确初始化为 0,并在绘制之后(或之前)更改回 0。
-
你必须用更多的代码来支持我们。代码现在的方式,只应该绘制具有有效状态的代码,并且方法的顺序没有区别,因此它们是条件块
-
@AndyFedoroff:我重新创建了要再次编译的项目。请参阅拉取请求。通过查看代码,我想知道为什么您对点而不是表示由点组成的形状的数据结构进行操作。
-
我认为这个问题受到XY-Problem的影响。
-
@AndyFedoroff,仅仅因为你没有让它工作,并不意味着它根本不起作用。
标签: macos if-statement swift2 drawrect cgcontext