多个彩色贝塞尔路径 [英] Multiple coloured bezier paths

查看:147
本文介绍了多个彩色贝塞尔路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的免费手绘图的代码。但是当我画的路径,以前的路径消失了。我不能弄清楚为什么会发生。有谁能够帮助我。这是我的代码。

Here is my code for free hand drawing. But when i draw the path, previous path is disappeared. I am not able to figure it out why is it happening so. Can any body help me. Here is my code.

- (void)drawRect:(CGRect)rect
{

for (NSMutableDictionary *dictionary in pathArray) {

    UIBezierPath *_path = [dict objectForKey:@"Path"];

    UIColor *_colors = [dict objectForKey:@"Colors"];

    [_colors setStroke];

    _path.lineCapStyle = kCGLineCapRound;

    [_path stroke];

  }

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {

isEdited=YES;

myPath=[[UIBezierPath alloc]init];

myPath.lineWidth=lineWidths;

CGPoint touchPoint = [[touches anyObject] locationInView:self];

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];

[myPath moveToPoint:[mytouch locationInView:self]];

[myPath addLineToPoint:CGPointMake(touchPoint.x+1, touchPoint.y+1)];

[dict setObject:myPath forKey:@"Path"];

[dict setObject:brushPattern forKey:@"Colors"];

[pathArray addObject:dict];

[self setNeedsDisplay];


 }

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];

[myPath addLineToPoint:[mytouch locationInView:self]];

[self setNeedsDisplay];

 }


推荐答案

每次创建 myPath dict localy in touchesBegan:燃烧。

You should create myPath and dict localy in touchesBegan: each time it's fired. Ditch their class-wide definitions.

对于更简单(更快)的性能,你仍然可以有类的 currentPath currentDict
ivars用于 touchesMoved:

For simpler (faster) performance you can still have class-wide currentPath and currentDict ivars for usage in touchesMoved:

EDIT:代码看起来像这样:

code would look something like this:

//currentPath declared as an iVar of UIBezierPath* type
//currentDict declared as an iVar of NSMutableDictionary* type


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{    
    isEdited=YES;

    UIBezierPath *myPath=[[UIBezierPath alloc]init];  //locally created

    myPath.lineWidth=lineWidths;

    CGPoint touchPoint = [[touches anyObject] locationInView:self];

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];

    [myPath moveToPoint:[mytouch locationInView:self]];

    [myPath addLineToPoint:CGPointMake(touchPoint.x+1, touchPoint.y+1)];

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init]; //locally created

    [dict setObject:myPath forKey:@"Path"];

    [dict setObject:brushPattern forKey:@"Colors"];

    [pathArray addObject:dict];

    [self setNeedsDisplay];

    currentPath = myPath;
    currentDict = dict;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [currentPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];
}

这篇关于多个彩色贝塞尔路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆