我有一张图片,我想拥有它,所以当我点击图片的某个部分时,只有该部分突出显示。目前为了测试这个,我已经设置了一个应用程序并在背面放置了一个图像视图。我刚刚创建了一个图像并在其上写了1,2,3和4。然后我使用UIView在图像中绘制水平和垂直线
我已经设置了一个轻触手势监听器,并打印出我已经击中的部分。代码
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
// Get the location of the touch
CGPoint touchPoint = [recognizer locationOfTouch:0 inView:[self view]];
if(touchPoint.x > 10 && touchPoint.x < 160)
{
if(touchPoint.y > 30 && touchPoint.y < 220)
{
NSLog(@"Button 1");
}else if(touchPoint.y > 220 && touchPoint.y < 430){
NSLog(@"Button 3");
}
}else if(touchPoint.x > 160 && touchPoint.x < 310)
{
if(touchPoint.y > 220 && touchPoint.y < 430)
{
NSLog(@"Button 4");
}else if(touchPoint.y > 30 && touchPoint.y < 220){
NSLog(@"Button 2");
}
}
//For debugging
// NSLog(@"x = %f y = %f", touchPoint.x, touchPoint.y);
}
现在我想要发生的是当我点击某个特定部分时,我想要突出显示该部分图像。这可能吗?目前我正在使用的解决方法是在网格顶部添加4个按钮,这些按钮隐藏为正常状态,并为它们提供一个突出显示状态的图像
[button setBackgroundImage:[UIImage imageNamed:@"highlightImage.png"] forState:UIControlStateHighlighted];
并将alpha设置为0.5或左右,因此看起来该部分正在突出显示。如果这是唯一可以做到的方式,那么只需留下一个答案就这样说,我会接受它,但如果一个可用,我宁愿选择一个更具体的解决方案!提前致谢
创建的图像示例
答案 0 :(得分:1)
看起来是正确的方法。
也许您应该在代码中使用较少的硬编码数字,并在更一般的级别上实现此功能(例如,使用图像视图或按钮的frame
属性)。
答案 1 :(得分:1)
我相信,你可以通过升级层次结构并使用UIControl来免费获得此行为。对其进行子类化并修改其突出显示行为。