2

I have created a subclass called Lines, that inherits from C4Shape. For now, all it does is create a random line when a class method is called.

The intention is that every new line begins at the previous line's pointB (i.e. end point) in order to make a continuing tree of lines. Eventually, I'll have multiple lines spawn from the same end point, old ones disappear, etc. Here's the code I have so far:

+(Lines *)createLineFromPoint:(CGPoint)startPoint {
CGPoint endPoint = CGPointMake([C4Math randomIntBetweenA:(startPoint.x-50) andB:(startPoint.x+50)],
                               [C4Math randomIntBetweenA:(startPoint.y-50) andB:(startPoint.y+50)]);
CGPoint linePoints[2] = {startPoint, endPoint};
Lines *newLine = [Lines new];
[newLine line:linePoints]; //This should make newLine a line type, should it not?
newLine.lineWidth = 3.0f;

return newLine;
}

-(void) setup {
[self performSelector:@selector(continueMakingLinesWithLine) withObject:(self) afterDelay:(3.0)];
}

-(void) continueMakingLinesWithLine {
[self.arrayOfLines addObject:[Lines createLineFromPoint:self.pointB]];
}

As far as I can tell, it should loop after the first call to continueMakingLinesWithLine in the C4WorkSpace; the first call is instantiated with a randomly generated CGPoint.

However, I am having a really hard time properly accessing the pointB property that was set during the last call to the method.

I received an error that tells me that it is because the C4Shape*(the Lines*) is not of type lines or arc.

However, the instance method should be making it of that type, shouldn't it?

4

1 回答 1

1

我不确定您的实际错误是什么意思。不过你的逻辑是很正确的。

首先,在您的代码中,您有以下内容:

[newLine line:linePoints]; //This should make newLine a line type, should it not?

答案是YES,而且是!

我将您的代码复制到一个新项目中,并在创建画布时将每一行添加到画布中。跑了,虽然不是我预期的方式。然而,这不是你的错。

您实际上在 [shapeObj line:...] 方法的实现中发现了一个小错误。

我将修复 GitHub 上 C4iOS 项目中的错误,但是使用您当前的安装程序,这不会出现在您的项目中。

解决方法

您可以执行以下两项操作之一:

1)C4Shape从您的行子类返回并使用初始化它[C4Shape line:linePoints]

2) 将以下内容添加到line 400of 中C4Shape.m

if(CGRectEqualToRect(CGRectZero, self.frame)) { self.frame = lineRect; }

1)的方法应如下所示:

+(Lines *)createLineFromPoint:(CGPoint)startPoint {
    CGPoint endPoint = CGPointMake([C4Math randomIntBetweenA:.. andB:..],
                                   [C4Math randomIntBetweenA:.. andB:..]);
    CGPoint linePoints[2] = {startPoint, endPoint};
    C4Shape *newLine = [C4Shape line:linePoints];
    newLine.lineWidth = 3.0f;
    return (Lines *)newLine;
}

对于2)方法的结尾,C4Shape.m应该如下所示:

-(void)_line:(NSArray *)pointArray {
    //Default implementation
    //..
    //..
    newBounds.origin = CGPointZero;
    if(CGRectEqualToRect(CGRectZero, self.frame)) { self.frame = lineRect; }
    CGPathRelease(newPath);
    _initialized = YES;
}

如果你这样做2),你不需要修复你当前的+(Lines *)方法。

其他事情

我收紧了您在上面发布的代码,如下所示:

@interface C4WorkSpace ()
@property NSMutableArray *arrayOfLines;
@end

@implementation C4WorkSpace

-(void) setup {
    self.arrayOfLines = [@[] mutableCopy];
    Lines *newLine = [Lines createLineFromPoint:self.canvas.center];
    [self.arrayOfLines addObject:newLine];
    [self.canvas addShape:newLine];
    [self continueMakingLinesWithLine];
}

-(void) continueMakingLinesWithLine {
    CGPoint p = ((Lines *)[self.arrayOfLines lastObject]).pointB;
    Lines *newLine = [Lines createLineFromPoint:p];
    [self.arrayOfLines addObject:newLine];
    [self.canvas addShape:newLine];
    [self runMethod:@"continueMakingLinesWithLine" afterDelay:1.0f];
}
@end

setup我创建第一行并将其添加到画布和数组中。然后,我让continueMakingLinesWithLine方法找到上一行,构建一个新的,然后再次调用它自己。

于 2013-11-03T06:54:11.397 回答