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

标签 ios xcode swift

我将在 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/

相关文章:

ios - 组合数组中的元素

ios - 返回前台时重新启动动画

ios - PickerView 不显示选择分隔符 - iOS

ios - 苹果推送通知的可靠性

javascript - CSS/JS 没有在 IOS 上加载,似乎没有原因,无法复制问题

c++ - 优化编译器消除错误

ios - 没有为 Xcode 6.1 编译的架构

objective-c - 触摸详细信息披露指示器时 Mapview 注释崩溃

ios - 如何从崩溃日志中调试 iOS 设备上的崩溃

iphone - iOS 6 应用商店 : URL to search for apps corresponding to defined keywords on Appstore?