Zeichnen von Linien in Swift

Ich zeichne eine horizontale Linie und eine vertikale Linie auf einem UITableViewCell verwenden Sie den folgenden code und es funktioniert einwandfrei auf iOS7. Seine in Objective-C.

In der Unterklasse der,

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

Und es sieht so aus,

Zeichnen von Linien in Swift

Ich versuche, die Umsetzung der gleichen in Swift. Aber diese Zeilen werden nicht angezeigt. Hier ist mein code bisher,

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

Mehrere Fehler kommen in den ersten block des Codes. Die Fehlermeldung ist 'AnyObject' nicht konvertierbar 'CGFloat'.
Keine Fehler im zweiten block aber immer noch die Zeile nicht angezeigt.

Kann mir bitte jemand sagen, was fehlt mir hier?

Danke.

Ich nehme an, es ist die Zeile mit f = selbst.Spalten[i]. Versuchen casting zu schweben anstatt let f = selbst.Spalten[i] wie? float. Nicht versuchen, mich selbst. Ich bin SEHR neu in Swift zu.
ist die meisten auf jeden Fall Recht, der Typ von f ist falsch. Sollten Sie mit [CGFloat] als Typ für das selbst.Spalten-array anstelle von NSArray
Hi, danke Euch beiden für die Antworten. Ich änderte die Art der array wie folgt var columns: [CGFloat] = [] geändert und der Fehler wurflinie zu diesem let f = self.columns[i] as? float aber ich bekomme eine neue Fehlermeldung besagt, dass das Modul 'float' - Typ. Ich habe auch vergessen zu erwähnen, dass es gibt eine Methode, die fügt Objekte zu den Spalten-array. Ich habe versucht zu übersetzen, um eine Rasche und habe einen anderen Fehler auch. Ich aktualisierte den code-snippet entsprechend in meinem ursprünglichen post.

InformationsquelleAutor Isuru | 2014-07-17

Schreibe einen Kommentar