将参数传递给UIView类中的函数时出错

时间:2015-07-30 20:17:16

标签: ios swift uiview

我试图将参数(坐标)传递给drawRect函数的覆盖。我不确定为什么参数没有被传递给函数

class Draw2D: UIView {
var startCord: CGPoint?
init(startCord: CGPoint) {
    self.startCord = startCord
    super.init(frame: CGRect())
}

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

}

override func drawRect(rect: CGRect)
{
    let context = UIGraphicsGetCurrentContext()
    let locations: [CGFloat] = [0.0, 1.0]

    let colors = [UIColor.whiteColor().CGColor,
        UIColor.blueColor().CGColor]

    let colorspace = CGColorSpaceCreateDeviceRGB()

    let gradient = CGGradientCreateWithColors(colorspace,
        colors, locations)


    var endPoint = CGPoint()

    endPoint.x = 200
    endPoint.y = 200
    let startRadius: CGFloat = 0
    let endRadius: CGFloat = 75

    CGContextDrawRadialGradient (context, gradient, startCord!,
        startRadius, endPoint, endRadius,
        0)



    }

}

这是我通过按钮操作

的参数
@IBAction func skittle(sender: AnyObject) {


    let plsWork = Draw2D(startCord: CGPoint(x: 30, y: 30))

}

我收到此错误:

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

这段代码通常会在UIView中绘制一个圆圈,但我只是在按下UI按钮时尝试绘制它。如何将ViewController.swift中的参数传递给这个单独的Draw2D.swift?

0 个答案:

没有答案