Ios 如何创建具有超过1000条边/线的可移动和可调整大小的多边形?

Ios 如何创建具有超过1000条边/线的可移动和可调整大小的多边形?,ios,objective-c,uibezierpath,polygons,Ios,Objective C,Uibezierpath,Polygons,现在我在视图的drawRect中使用UIBezierPath和moveToPoint/addLineToPoint。 该视图从viewController接收触摸移动的。它修改绘制多边形时使用的posx和posy变量,如下所示: [path addLineToPoint:CGPointMake([p.getx floatValue]+posx, [p.gety floatValue]+posy)] 不幸的是,性能很糟糕,每次我移动多边形时,它都会留下痕迹 实现我想要做的事情的最佳方式是什么 编

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

[path addLineToPoint:CGPointMake([p.getx floatValue]+posx, [p.gety floatValue]+posy)]
不幸的是,性能很糟糕,每次我移动多边形时,它都会留下痕迹

实现我想要做的事情的最佳方式是什么

编辑:drawRect
poly
是一个包含
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];
}
-(void)drawRect:(CGRect)rect{
UIBezierPath*路径;
UIColor*fillColor;
路径=[UIBezierPath bezierPath];
对于(int i=0;i<[polys count];i++){
poly*p=[polys objectAtIndex:i];
如果(i==0){
[path moveToPoint:CGPointMake([p.getx floatValue]+posx[p.gety floatValue]+posy)];
}否则{
[path addLineToPoint:CGPointMake([p.getx floatValue]+posx[p.gety floatValue]+posy)];
fillColor=[UIColor blueColor];//计划在此处使用随机颜色
}
}
[path closePath];
[填充颜色设置填充];
[路径填充];
}

还没有解决您的问题。我猜你想用用户的手指画多边形。我有一个很好用的小班,也许它可以帮助:

@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
@实现视图{
NSMutableArray*_点;
}
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
self.backgroundColor=[UIColor whiteColor];
_points=[NSMutableArray];
回归自我;
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
//扫清老路
[_点移除所有对象];
UITouch*touch=[触摸任何对象];
CGP点=[触摸位置视图:自];
[\u points addObject:[NSValue valueWithCGPoint:p]];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
UITouch*touch=[触摸任何对象];
CGP点=[触摸位置视图:自];
[\u points addObject:[NSValue valueWithCGPoint:p]];
[自我设置需要显示];
}
-(void)drawRect:(CGRect)rect
{
UIBezierPath*路径=[UIBezierPath bezierPath];
对于(int i=0;i<\u points.count;i++){
CGPoint=[[u点[i]CGPointValue];
如果(i==0){
[路径移动点:p];
}
否则{
[path addLineToPoint:p];
}
}
[path closePath];
UIColor*color=[UIColor blueColor];
[颜色设置填充];
[路径填充];
}
@结束

只需将视图添加到应用程序中的某个位置,或许可以使其全屏显示。

您可以发布更多的代码吗?整个
drawRect
方法?是的,我可以。刚刚编辑过。多边形已经生成了。我想使用能够移动和调整大小(捏)它。然而,我有大约60个多边形,每个多边形有超过1000条边。当我试着移动它时,它的表现很糟糕。嗯,总共有60000个边。。。将每个多边形转换成位图并缩放/旋转/平移如何?那太好了。是否有一种方法可以自动将多边形转换为位图?您知道有任何教程解释如何操作吗?您可以从使用
UIGraphicsBeginImageContext
创建图像开始,使用
UIGraphicsGetCurrentContext
获取
CGContextRef
。在图像上绘图是可以用的。绘制后,可以使用
UIGraphicsGetImageFromCurrentImageContext
提取
UIImage*
。要计算出图像的大小以及到每个点的偏移量需要相当多的数学知识。不确定您是否熟悉核心图形(也称为Quartz 2D),但这与您使用代码所做的类似,只是在较低的级别。如果你感兴趣,可以看看,也许是关于你的问题的路径部分。