开发者

Have UITouch only perform task when touching certain color

How can I have UITouch recognize and perform a certain task ONLY when I touch a certain color in a UIImage view.

For example:

I have an image of a street. The street has greyish buildings and a white road. If I touch a building, I don't want anything to happen, but if I touch a white road, I want to highlight that section where I touched to a light blu开发者_运维技巧e.


  1. Capture touch events on your image
  2. Use the touch location to get the pixel color at that location.
  3. Decide whether the color represents Road or Building.

Get Exact Touch Coordinates

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView: imageView];

    // Here are your touch coordinates
    touchPoint.x
    touchPoint.y
}

Get Pixel Color at that location: code to get pixel color.

Decide if the color represents Road.

// 230 used as example, Experiment to find a good value
if (red >230 && green >230 && blue >230)
{
    // turn street blue
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜