使用UIBeizerpath无效工作在imageContext上绘制圆角矩形

时间:2013-01-19 05:15:38

标签: ios6 core-graphics

当用户触摸在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();
}

1 个答案:

答案 0 :(得分:0)

修改您的initWithFrame方法,使UIView白色背景。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}