0
我正在創建一個ipad應用程序,並且我有一個可以用紅色填充和藍色邊框創建矩形的按鈕。我想改變邊框的顏色,當我觸摸創建的矩形。我應該使用哪個聲明?謝謝如何在用戶觸摸屏幕時更改uiview顏色
我正在創建一個ipad應用程序,並且我有一個可以用紅色填充和藍色邊框創建矩形的按鈕。我想改變邊框的顏色,當我觸摸創建的矩形。我應該使用哪個聲明?謝謝如何在用戶觸摸屏幕時更改uiview顏色
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);
}