【问题标题】:Drawing lines in Swift在 Swift 中画线
【发布时间】:2014-07-17 11:51:30
【问题描述】:

我正在使用以下代码在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]];
}

它看起来像这样,

我正在尝试在 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'
}

代码的第一块出现多个错误。错误消息是 'AnyObject' 不能转换为 'CGFloat'。 第二个块没有错误,但仍然没有出现该行。

有人可以告诉我我在这里缺少什么吗?

谢谢。

【问题讨论】:

  • 我想是 let f = self.columns[i] 的那一行。尝试转换为 float 而不是 let f = self.columns[i] as?漂浮。自己没试过。我对 Swift 也很陌生。
  • @Akaino 绝对正确,f 的类型是错误的。您应该使用 [CGFloat] 作为 self.columns 数组的类型,而不是 NSArray
  • 您好,谢谢您的回复。我像这样var columns: [CGFloat] = [] 更改了数组的类型,并将错误抛出行更改为let f = self.columns[i] as? float,但我收到一个新错误,提示 Use of module 'float' as a type。我还忘了提到有一种方法可以将对象添加到列数组中。我尝试将其翻译成 Swift,但也遇到了另一个错误。我在原始帖子中相应地更新了代码 sn-p。

标签: ios objective-c drawing swift uigraphicscontext


【解决方案1】:

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)
}

【讨论】:

  • 非常感谢。它工作得很好。我慢慢开始看到 Swift 好的一面。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多