如何创建一个具有超过1000个边/线的可移动且可调整大小的多边形?

时间:2013-06-23 23:46:27

标签: ios objective-c uibezierpath polygons

现在我在视图moveToPoint中使用UIBezierPath和addLineToPoint / drawRect。 该视图从viewController接收touchesMoved。它会修改绘制多边形时使用的posxposy变量,如下所示:

[path addLineToPoint:CGPointMake([p.getx floatValue]+posx, [p.gety floatValue]+posy)]

不幸的是,表演非常糟糕,每当我移动它时,多边形都会留下痕迹。

达到我想要做的最好的方法是什么?

编辑:drawRect。 polys是带有poly个对象的NSMutableArray。每个多边形是一个x / y点。

- (void)drawRect:(CGRect)rect{
UIBezierPath* path;
UIColor* fillColor;
path = [UIBezierPath bezierPath];
for (int i = 0; i < [polys count]; i++){
    poly *p = [polys objectAtIndex:i];
    if (i == 0){
        [path moveToPoint:CGPointMake([p.getx floatValue]+posx, [p.gety floatValue]+posy)];
    }else{
        [path addLineToPoint:CGPointMake([p.getx floatValue]+posx, [p.gety floatValue]+posy)];
        fillColor = [UIColor blueColor]; // plan to use a random color here
        }
    }
[path closePath];
[fillColor setFill];
[path fill];
}

1 个答案:

答案 0 :(得分:1)

Haven没弄明白你的问题。我的猜测是你想用用户的手指画出多边形。我有一个完美的小班,可能会有所帮助:

@implementation View {
    NSMutableArray* _points;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    self.backgroundColor = [UIColor whiteColor];

    _points = [NSMutableArray array];

    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // Clear old path
    [_points removeAllObjects];

    UITouch* touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];

    [_points addObject:[NSValue valueWithCGPoint:p]];
}

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

    [_points addObject:[NSValue valueWithCGPoint:p]];

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    UIBezierPath* path = [UIBezierPath bezierPath];

    for (int i = 0; i < _points.count; i++){
        CGPoint p = [_points[i] CGPointValue];
        if (i == 0){
            [path moveToPoint:p];
        }
        else {
            [path addLineToPoint:p];
        }
    }

    [path closePath];

    UIColor* color = [UIColor blueColor];
    [color setFill];
    [path fill];
}

@end

只需在应用中的某处添加视图,也可以全屏显示。