0

我正在创建一个 ipad 应用程序,我有一个按钮可以创建一个带有红色填充和蓝色边框的矩形。当我触摸创建的矩形时,我想更改边框颜色。我应该使用哪个语句?谢谢

4

1 回答 1

0

yourRect 和borderColor 是iVar。您必须为每次触摸矩形时更改颜色的逻辑编写方法 changeBorderColor。

- (void) init
{
  yourRect = CGRectMake(100,100, 200,200);
  borderColor = [UIColor blueColor];

  UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleTap.numberOfTapsRequired = 1;     
    [self addGestureRecognizer:singleTap:];
    [singleTap release];
}

-(void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint tapLocation = [gestureRecognizer locationInView:shareView];

    if(CGRectContainsPoint(yourRect, tapLocation))
    {
        borderColor = [self changeBorderColor]; //Change color
    }
}

- (void)drawRect:(CGRect)rect;
{   
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextFillRect(context, yourRect);

    CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
    CGContextStrokeRect(context, yourRect);
}
于 2012-07-06T19:05:37.390 回答