在Swift中绘制线条

时间:2014-07-17 11:51:31

标签: ios objective-c drawing swift uigraphicscontext

我使用以下代码在UITableViewCell上绘制水平线和垂直线,它在iOS7上运行正常。它在Objective-C。

的子类中
@property (strong, nonatomic) NSMutableArray *columns;

- (void)drawRect:(CGRect)rect
{
    // In iOS7, you have to set the cell's background color to clear otherwise the drawing lines won't appear
    self.backgroundColor = [UIColor clearColor];

    // Drawing the vertical line
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
    CGContextSetLineWidth(ctx, 0.75);
    for (int i = 0; i < self.columns.count; i++) {
        CGFloat f = [((NSNumber*) [self.columns objectAtIndex:i]) floatValue];
        CGContextMoveToPoint(ctx, f, 10);
        CGContextAddLineToPoint(ctx, f, self.bounds.size.height - 10);
    }
    CGContextStrokePath(ctx);

    // Drawing the horizontal line
    CGContextRef cntx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
    CGContextSetLineWidth(cntx, 1);
    for (int i = 0; i < self.columns.count; i++) {
        CGContextMoveToPoint(cntx, 15, self.bounds.size.height / 2);
        CGContextAddLineToPoint(cntx, 60, self.bounds.size.height / 2);
    }
    CGContextStrokePath(cntx);

    [super drawRect:rect];
}

- (void)addColumn:(CGFloat)position
{
    [self.columns addObject:[NSNumber numberWithFloat:position]];
}

它看起来像这样,

enter image description here

我试图在Swift中实现相同的功能。然而,这些线不会出现。到目前为止,这是我的代码,

var columns: [CGFloat] = []

override func drawRect(rect: CGRect)  {

    // Drawing the vertical line
    let ctx = UIGraphicsGetCurrentContext()
    CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0)
    CGContextSetLineWidth(ctx, 0.75)
    for var i = 0; i < self.columns.count; i++ {
        let f = self.columns[i] as? float // Error - Use of module 'float' as a type
        CGContextMoveToPoint(ctx, f, 10)
        CGContextAddLineToPoint(ctx, f, self.bounds.size.height - 10)
    }
    CGContextStrokePath(ctx)

    // Drawing the horizontal line
    let cntx = UIGraphicsGetCurrentContext()
    CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0)
    CGContextSetLineWidth(cntx, 1)
    for var i = 0; i < self.columns.count; i++ {
        CGContextMoveToPoint(cntx, 15, self.bounds.size.height / 2)
        CGContextAddLineToPoint(cntx, 60, self.bounds.size.height / 2)
    }
    CGContextStrokePath(cntx)

    self.backgroundColor = UIColor.clearColor()

    super.drawRect(rect)
}

func addColumn(position: CGFloat) {
    self.columns.append(NSNumber.numberWithFloat(position)) // Error - 'NSNumber' is not a subtype of 'Float'
}

代码的第一个块出现多个错误。错误消息是&#39; AnyObject&#39;不能转换为&#39; CGFloat&#39; 。 第二个区块没有错误,但线条仍然没有出现。

有人可以告诉我我在这里失踪了吗?

谢谢。

2 个答案:

答案 0 :(得分:4)

Objective-C只能在NSArray中存储对象,这就是浮点值必须包含在NSNumber中的原因。在Swift中,这更容易,因为您可以直接存储浮动。既然您的columns数组属于[CGFloat]类型,您只需附加值而不将其包装为NSNumber

func addColumn(position: CGFloat) {
  self.columns.append(position)
}

您不需要转换来自columns数组的值,因为它们已经浮动:

for f in self.columns {
  CGContextMoveToPoint(ctx, f, 10)
  CGContextAddLineToPoint(ctx, f, self.bounds.size.height - 10)
}

答案 1 :(得分:-3)

我相信这个http://www.raywenderlich.com/87899/make-simple-drawing-app-uikit-swift汇总了有关此主题的更多见解