2012-07-05 112 views

回答

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); 
} 
相關問題