ios - 在 Swift 中画线

标签 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'
}

代码的第一 block 出现了多个错误。错误信息是'AnyObject' is not conversionble to 'CGFloat'。 第二个 block 中没有错误,但仍然没有出现该行。

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

谢谢。

最佳答案

Objective-C 只能将对象存储在 NSArray 中,这就是浮点值必须包含在 NSNumber 中的原因。在 Swift 中,这要容易得多,因为您可以直接存储 float 。现在您的 columns 数组是 [CGFloat] 类型,您可以简单地附加值而不将其包装为 NSNumber:

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

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

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

关于ios - 在 Swift 中画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802771/

相关文章:

c# - 在 C# 中绘制视频

ios - FaSTLane TestFlight 构建

iphone - 更改 UITable 部分标题颜色

iphone - 如何在 iOS 中使 uiscrollview 无限?

android - 自定义 View 扩展 View 类但仍然基于 XML 布局

c# - 使用 ZedGraph lib 在图形上隐藏 X 轴

ios - 设置 iOS 导航背景颜色无法正常工作

iOS UITests - 如何获取当前 View 中存在的 XCUIElement 列表

iphone - 让 AVFoundation 不停止 iPod 音乐

iphone - 使用 Restkit 为 NSManagedObject 类创建实例