当用户触摸在 iPad 上移动时,我需要绘制一个圆角矩形。这是我的代码,它在触摸移动时显示黑色圆角矩形,在触摸结束时显示选定颜色。谁能告诉我原因?
- (void)drawRect:(CGRect)rect
{
[path setLineWidth:lineSize];
[selectedColor setStroke];
[drawImage drawInRect:rect]; // (3)
[path fill];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
startPoint=p;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
float x=startPoint.x;
float y=startPoint.y;
float z=p.x-startPoint.x;
float a=p.y-startPoint.y;
if (z<0 && a<0) {
x=p.x;
y=p.y;
z=startPoint.x-p.x;
a=startPoint.y-p.y;
}
else if (a<0)
{
x=startPoint.x;
y=startPoint.y;
z=p.x-startPoint.x;
a=startPoint.y-p.y;
}
else if(z<0)
{
x=startPoint.x;
y=startPoint.y;
z=startPoint.x-p.x;
a=p.y-startPoint.y;
}
path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x,y,z,a) cornerRadius:10.0];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[self drawBitmap];
[self setNeedsDisplay];
}
- (void)drawBitmap // (3)
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
[selectedColor setStroke];
if (!drawImage) // first draw; paint background white by ...
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
[[UIColor whiteColor] setFill];
[rectpath fill]; // filling it with white
}
[drawImage drawAtPoint:CGPointZero];
[selectedColor setFill];
[path fill];
drawImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}