2

これが私のフリーハンド描画用のコードです。しかし、パスを描くと、前のパスが消えてしまいます。なぜそうなるのか、私には理解できません。どんな体でも私を助けることができますか?これが私のコードです。

- (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];

 }
4

2 に答える 2

0

起動するたびに作成myPathしてdictローカライズする必要があります。touchesBegan:クラス全体の定義を捨てます。

より単純な (より高速な) パフォーマンスのために、クラス全体currentPathcurrentDict ivar を使用することができます。touchesMoved:

編集: コードは次のようになります。

//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];
}
于 2012-06-01T09:37:10.753 に答える