gpt4 book ai didi

macos - 使 2 个相互矛盾的方法在 drawRect 中起作用

转载 作者:行者123 更新时间:2023-12-04 18:30:07 24 4
gpt4 key购买 nike

我正在编写一个由 CGPoints 组成的应用程序构建元素。我有 2 个按钮:makeRectanglemakeTriangle .对于构建/绘图阶段,我使用三种方法 矩形以及 的三种方法三角形drawRect .

我的代码被困在 drawRect .在 if-else-statement每次按下按钮时,每种方法都会为前一个元素交换建筑/绘图方案。

如果我已经建立了矩形 然后我点击 makeTriangle按钮,我得到了新的三角形,但我的矩形变成了一个有一个未连接点的三角形。

是否有解决方法 否则我不应该使用 drawRect方法?

这是 drawRect 上的 SO 帖子主题:To drawRect or not to drawRect

Animation of incorrectly-drawn trapezoid and triangle

Image of correctly-drawn trapezoid and triangle

元素声明:

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

最佳答案

if (makeTriangle != nil) {if (makeRectangle != nil) {没有多大意义。根据您的评论,makerRectanglemakeTriangle是按钮。通过你的语句,你正在检查它们的存在——我们可以假设它们总是存在——第一个 if 子句将始终被执行。

你想要的是:创建将由按钮执行的方法。每个方法都将设置 bool 值的组合或单个枚举值,然后通过调用 setNeedsDisplay() 告诉 View 重绘.

关于macos - 使 2 个相互矛盾的方法在 drawRect 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39855349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com