터치하여 화면에 도형을 그릴 응용 프로그램을 만들려고합니다.도형을 그릴 때 선이 지워집니다.
한 점에서 다른 점까지 선을 그릴 수는 있지만 새로운 각 그리기마다 지워집니다.
CGPoint location;
CGContextRef context;
CGPoint drawAtPoint;
CGPoint lastPoint;
-(void)awakeFromNib{
//[self addSubview:noteView];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
location = [touch locationInView:touch.view];
[self setNeedsDisplayInRect:CGRectMake(0, 0, 320, 480)];
}
- (void)drawRect:(CGRect)rect {
context = UIGraphicsGetCurrentContext();
[[UIColor blueColor] set];
CGContextSetLineWidth(context,10);
drawAtPoint.x =location.x;
drawAtPoint.y =location.y;
CGContextAddEllipseInRect(context,CGRectMake(drawAtPoint.x, drawAtPoint.y, 2, 2));
CGContextAddLineToPoint(context,lastPoint.x, lastPoint.y);
CGContextStrokePath(context);
lastPoint.x =location.x;
lastPoint.y =location.y;
}
이 도움말 -
Nir 씨가 감사합니다 :
여기 내 코드입니다.
나는 UILayers 대신에 CALayers를 의미한다고 생각합니다. 그는 여전히 CALayer의 drawInContext : 메소드 또는 delegate-drawLayer : inContext : 메소드에서 사용자 정의 드로잉을 수행해야하지만 레이어가 다시 그릴 때까지 해당 레이어가 내용을 보존하게됩니다. –
감사합니다, 당신의 권리 - 나는 대답을 받았다. –