2012-06-21 2 views
3

손가락 페인트 응용 프로그램 용 사용자 정의 UIView (.xib 제외)를 만들었습니다.iPhone의 사용자 정의 UIView에서 손가락 페인트를 지우는 방법

페인트는 사용자 정의의 UIView와 잘 작동하지만 내 문제는 내가 무엇입니까 그려진 경로 삭제하려고 할 때이다 :

오류 : 잘못된 상황 아래

을 내 클래스 :

를 나는이 시대를 설정 한

@interface draw2D : UIView 
{ 
    CGPoint previousPoint; 
    CGPoint lastPoint; 
    CGMutablePathRef path; 
    UIButton *btnClose; 
    UIButton *btnErase; 
    BOOL IsErase; 
} 
- (IBAction)btnClose:(id)sender; 
- (IBAction)btnErase:(id)sender; 
@end 


@implementation draw2D 
- (void)awakeFromNib 
{ 
    path = CGPathCreateMutable(); 
} 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     btnClose = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [btnClose addTarget:self action:@selector(btnClose:) 
     forControlEvents:UIControlEventTouchDown]; 
     [btnClose setTitle:@"close" forState:UIControlStateNormal]; 
     btnClose.frame = CGRectMake(10, 10, 100, 40.0); 

     btnErase = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [btnErase addTarget:self action:@selector(btnErase:) 
      forControlEvents:UIControlEventTouchDown]; 
     [btnErase setTitle:@"Erase" forState:UIControlStateNormal]; 
     btnErase.frame = CGRectMake(150, 10, 100, 40.0); 

     [self addSubview:btnClose]; 
     [self addSubview:btnErase]; 
    } 
    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    UITouch *touch = [touches anyObject]; 
    NSLog(@"Touch Began :%d",[touch tapCount]); 

    if ([touch tapCount] > 1) 
    { 
     NSLog(@"::::: Paint Start :::::"); 
     path = CGPathCreateMutable(); 
     previousPoint = lastPoint; 
     [self setNeedsDisplay]; 
    } 
    self.backgroundColor = [UIColor clearColor]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"::::: touchesMoved :::::"); 
    lastPoint = [[touches anyObject] locationInView:self]; 
    previousPoint = [[touches anyObject] previousLocationInView:self]; 

    if(IsErase) 
    { 
     NSLog(@"erase"); 
     UITouch *erasetouch = [touches anyObject]; 
     CGPoint erasecurrentPoint = [erasetouch locationInView:self]; 

     CGContextRef erasecontext = UIGraphicsGetCurrentContext(); 
     CGContextSetLineCap(erasecontext, kCGLineCapRound); 
     CGContextSetLineWidth(erasecontext,10); 
     CGContextSetBlendMode(erasecontext, kCGBlendModeClear); 
     CGContextSetStrokeColorWithColor(erasecontext, [[UIColor clearColor] CGColor]); 
     CGContextBeginPath(erasecontext); 
     CGContextMoveToPoint(erasecontext, lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(erasecontext, erasecurrentPoint.x, erasecurrentPoint.y); 
     CGContextStrokePath(erasecontext);  
     CGContextFlush(erasecontext); 

    } 

    [self setNeedsDisplay]; 
} 
- (void)drawRect:(CGRect)rect 
{ 
    NSLog(@"::::: drawRect :::::"); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGPathMoveToPoint(path, NULL, previousPoint.x, previousPoint.y); 
    CGPathAddLineToPoint(path, NULL, lastPoint.x, lastPoint.y); 
    CGContextAddPath(context, path); 
    CGContextSetLineWidth(context, 5); 
    [[UIColor blueColor] setStroke]; 
    CGContextDrawPath(context, kCGPathFillStroke); 
} 

- (IBAction)btnClose:(id)sender 
{ 
    [self removeFromSuperview]; 
} 
- (IBAction)btnErase:(id)sender 
{ 
    IsErase = YES; 
}  

@end 

.H 파일 기능이 있지만 작동하지 않는 버튼.

+0

이가 정말 작품이 답변을 확인하시기 바랍니다 완벽하게 http://stackoverflow.com/questions/3863931/want-to-add-manual-erasing-option-in-ipad-painting-application- by-quartz/12797513 # 12797513 –

답변

1

마침내 내가 해결책을 찾을 수 있습니다

당신은 부드러운 도면에 대한 구현을 제공이 github의의의 repo에서 좀 걸릴 수 있습니다. 내 실수는 새로운 contexttouchMove 메서드에서 지우기 코드를 구현했다. 나는 새것이 필요 없다. context. drawrect 메서드에서 코드를 지우고 이제는 잘 작동합니다. 아래 코드를 참조하십시오.

- (void)drawRect:(CGRect)rect 
{ 

    [curImage drawAtPoint:CGPointMake(0, 0)]; 
    CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2 = midPoint(currentPoint, previousPoint1); 

    context = UIGraphicsGetCurrentContext(); 
    [self.layer renderInContext:context]; 

    if(IsErase) 
    { 
     CGContextSetLineWidth(context,self.lineWidth); 
     CGContextSetBlendMode(context, kCGBlendModeClear); 
     CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]); 
     CGContextBeginPath(context); 
     CGContextMoveToPoint(context, mid1.x, mid1.y); 
     CGContextAddLineToPoint(context, previousPoint1.x, previousPoint1.y); 
     CGContextStrokePath(context);  
     CGContextFlush(context); 
    } 
    else 
    { 
     CGContextMoveToPoint(context, mid1.x, mid1.y); 
     CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 
     CGContextSetLineCap(context, kCGLineCapRound); 
     CGContextSetLineWidth(context, self.lineWidth); 
     CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); 
     CGContextSaveGState(context); 
     CGContextStrokePath(context); 
    } 
    [super drawRect:rect]; 
    [curImage release]; 
} 

사람이 지우는 기능에 도움이되기를 바랍니다.

2

당신이 가진 문제는 당신이 당신의 touchesBegan:withEvent:에서 drawRect:

UIGraphicGetContext() 외부 전화를 안하고 touchesMoved:withEvent: 당신은 단순히 당신이 그리는 당신이 지금하고있는대로 [self setNeedsDisplay]를 호출 할 포인트를 저장해야한다는 것입니다. drawRect: 구현에서는 저장 한 포인트를 그립니다. https://github.com/levinunnink/Smooth-Line-View

+0

감사합니다. 예를 들어 손가락 페인트 응용 프로그램을 만드는 데 도움이됩니다. 그렇지만 지우기 기능은 무엇입니까? github 데모로 구현하는 방법? – Hitarth

관련 문제