如何在用户触摸屏幕时更改uiview颜色

时间:2012-07-05 23:40:52

标签: ipad

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

1 个答案:

答案 0 :(得分:0)

yourRect和borderColor是iVars。你必须编写方法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);
}