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.
- Capture touch events on your image
- Use the touch location to get the pixel color at that location.
- 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
}
精彩评论