当用户触摸在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();
}
答案 0 :(得分:0)
修改您的initWithFrame
方法,使UIView白色背景。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
}
return self;
}