2016-01-25 4 views
-2

Скажем, у меня есть что-то подобное в Objective-CКак установить UIColor UIBezierPath в Swift?

- (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]; 
    } 
    } 
} 

Как бы сделать следующую строку [[UIColor colorWithWhite:0 alpha:alpha] set]; в Swift?

ответ

2

Вы можете написать 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) 
     } 
    } 
} 
Смежные вопросы