如何在 Swift 中设置 UIBezierPath 的 UIColor?

How can I set the UIColor of a UIBezierPath in Swift?

假设我在 Objective-C

中有这样的东西
 - (void)drawRect:(CGRect)rect {
  if (1 < [_points count]) {
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path setLineWidth:3.];
    MyPoint *point = _points[0];
    [path moveToPoint:point.where];
    NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];

    for (int i = 1; i < (int)_points.count; ++i) {
      point = _points[i];
      [path addLineToPoint:point.where];
      float alpha = 1;
      if (1 < now - point.when) {
        alpha = 1 - MIN(1, now - (1+point.when));
      }
      [[UIColor colorWithWhite:0 alpha:alpha] set]; //THIS LINE
      [path stroke];
      path = [UIBezierPath bezierPath];
      [path setLineWidth:3.];
      [path moveToPoint:point.where];
    }
  }
}

如何在 Swift 中执行以下行 [[UIColor colorWithWhite:0 alpha:alpha] set];

你可以写UIColor(white: 0, alpha: alpha).set()而不是[[UIColor colorWithWhite:0 alpha:alpha] set];

func drawRect(rect: CGRect) {
    if 1 < points.count {
        var path: UIBezierPath = UIBezierPath()
        path.lineWidth = 3.0
        var point: MyPoint = points[0]
        path.moveToPoint(point.where)
        var now: NSTimeInterval = NSDate.timeIntervalSinceReferenceDate()
        for var i = 1; i < Int(points.count); ++i {
            point = points[i]
            path.addLineToPoint(point.where)
            var alpha: Float = 1
            if 1 < now - point.when {
                alpha = 1 - min(1, now - (1 + point.when))
            }
            UIColor(white: 0, alpha: alpha).set()
            //THIS LINE
            path.stroke()
            path = UIBezierPath()
            path.lineWidth = 3.0
            path.moveToPoint(point.where)
        }
    }
}