使用NSpoint像素和数组绘制线条

时间:2012-02-08 09:52:34

标签: iphone objective-c

我将NSPoint存储在这样的数组中。我想将该数组输入提供给另一个类来绘制相同的操作。如何绘制特定的数组值。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self];

    CGPathAddLineToPoint(self.currentLine.linePath, NULL, currentPoint.x, currentPoint.y);
    [self setNeedsDisplay];

    [_array addObject:[NSValue valueWithCGPoint: currentPoint]];

}

这是我的数组值 控制台输出

"NSPoint: {745, 575}",
"NSPoint: {730, 584}",
"NSPoint: {717, 588}",
"NSPoint: {701, 590}",
"NSPoint: {678, 590}",
"NSPoint: {642, 590}",
"NSPoint: {590, 590}",
"NSPoint: {520, 590}",
"NSPoint: {465, 589}",
"NSPoint: {438, 587}",
"NSPoint: {415, 587}",
"NSPoint: {403, 582}"


       - (void)drawRect:(CGRect)rect
        {
//    CGContextSetAlpha(context, self.currentLine.opacity);    
//        CGContextSetStrokeColorWithColor(context, self.currentLine.lineColor.CGColor);
//        CGContextSetLineWidth(context, self.currentLine.lineWidth);
//        CGContextSetLineCap(context, kCGLineCapRound);
//        CGContextSetLineJoin(context, kCGLineJoinRound);
 //       CGContextBeginPath(context);
//        CGContextAddPath(context, self.currentLine.linePath);
 //       CGContextStrokePath(context);
???????????
        }

此代码将用于触摸输入线drawing.how绘制线与用户交互 当视图将apper。这个子类uiview。我能够使用mouse.my需要是我有CGPoint值数组我希望将该数组传递给此类输入以绘制line。我可以通过那个

3 个答案:

答案 0 :(得分:1)

您需要从数组中获取点值并从中创建路径,然后描绘路径。

- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // Set up your conext here, line colour, thickness, endcaps etc
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextSetLineWidth(ctx, 2.0);
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);

    //Create a path
    CGMutablePathRef pathRef = CGPathCreateMutable();

    // assume your array is called pointsArray and is a property
    // Move to the starting point
    CGPoint pt = [[self.pointsArray objectAtIndex:0] CGPointValue];
    CGPathMoveToPoint(pathRef, NULL, pt.x, pt.y);

    // Add the other points to the path
    CFIndex arrayCount = [self.pointsArray count];

    for (CFIndex i = 1; i < arrayCount; i++) {
        pt = [[self.pointsArray objectAtIndex:i] CGPointValue];
        CGPathAddLineToPoint(pathRef, NULL, pt.x, pt.y);
    }

    // Now you have a path, stroke it
    CGContextAddPath(ctx, pathRef);
    CGContextStrokePath(ctx);

    // Release your pathRef
    CGPathRelease(pathRef);
}

您可以下载显示此功能的小型Example Xcode project

答案 1 :(得分:0)

在您要传递数组的当前类中执行此操作

A.H

{
    NSArray *mArray;
}
@property (nonatomic, retain) NSArray *mArray;

A.m

@synthesize mArray;

然后在创建A对象

的位置写下这个
A *a = [[A alloc] init]];
//this code to set the array in your A class
a.mArray = yourArrayOfPoints;

答案 2 :(得分:0)

  CGContextMoveToPoint(context, 0.0, 0.0);
  for (NSValue* value in array)
    {
      CGPoint p =[value CGPointValue];
      CGContextAddLineToPoint(context,p.x,p.y);

    }

把它放进 - (void)drawInContext:(CGContextRef)上下文方法

尝试这可能对您有所帮助......