我将在iOS-Application中的视图上绘制线条。
xcode write,该行不能转换为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")
}
}
答案 0 :(得分:1)
CGContextXXX函数的第一个参数是您操作的CGContext。在您的情况下,您可能希望使用上面几行创建的上下文变量:
CGContextMoveToPoint(context, line.start.x, line.start.y)