现在我在视图moveToPoint
中使用UIBezierPath和addLineToPoint
/ drawRect
。
该视图从viewController接收touchesMoved
。它会修改绘制多边形时使用的posx
和posy
变量,如下所示:
[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];
}
答案 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
只需在应用中的某处添加视图,也可以全屏显示。