1

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();
}
4

1 に答える 1

0

メソッドを編集してinitWithFrame、UIView の背景を白にします。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
于 2014-04-23T12:26:55.767 に答える