gpt4 book ai didi

ios - Swift 中的变量无法转换为 CGContext

转载 作者:行者123 更新时间:2023-11-29 02:31:37 27 4
gpt4 key购买 nike

我将在 iOS 应用程序的 View 上画线。

xcode 写入,该行不能转换为 CGContext。

我应该转换行吗?当我必须做的时候,我希望你能给我提示。

这是我的 DrawView.swift:

class DrawView: UIView {

var lines: [Line] = []
var lastPoint: CGPoint!

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let t = touches.anyObject() as UITouch
lastPoint = t.locationInView(self)
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
let t = touches.anyObject() as UITouch
var newPoint = t.locationInView(self)
lines.append(Line(start: lastPoint, end: newPoint))

lastPoint = newPoint

self.setNeedsDisplay()
}

override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()

CGContextBeginPath(context)

for line in lines {
//Here is the error 'line' is not convertible to CGContext
CGContextMoveToPoint(line, line.start.x, line.start.y)
CGContextAddLineToPoint(line, line.end.x, line.end.y)
}

CGContextSetRGBStrokeColor(context, 1, 1, 0, 1)
CGContextSetLineWidth(context, 5)
CGContextStrokePath(context)

}
}

这是 Line.swift

import UIKit

class Line {

var start: CGPoint
var end: CGPoint

init(start _start: CGPoint, end _end: CGPoint) {
start = _start
end = _end
}


}

这是 ViewController.swift

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func clearTapped () {
NSLog("This button is tapped")
}

}

最佳答案

CGContextXXX 函数的第一个参数是您操作的 CGContext。在您的情况下,您可能想使用上面几行创建的上下文变量:

CGContextMoveToPoint(context, line.start.x, line.start.y)

关于ios - Swift 中的变量无法转换为 CGContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26782160/

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