很抱歉,如果这是一个基本问题,我找不到明确的答案。
我已经设置了4个按钮:
// Add the normal and selected state for each button
UIImage *buttonImage = [UIImage imageNamed:[NSString stringWithFormat:@"HotspotNumber2-%i.png",(hotspotID +1)]];
[hotspotButton setImage:buttonImage forState:UIControlStateNormal];
UIImage *buttonImageSelected = [UIImage imageNamed:[NSString stringWithFormat:@"HotspotNumber2-%is.png",(hotspotID +1)]];
[hotspotButton setImage:buttonImageSelected forState:UIControlStateSelected];
[hotspotButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[hotspotButton addTarget:self action:@selector(hotspotTouch:) forControlEvents:UIControlEventTouchDown];
我将触摸事件捕获在方法中:
// Called when a hotspot is touched
-(void)hotspotTouch:(id)sender{
// Deselect the hotspot currently selected
if (selectedHotspot) [selectedHotspot setSelected:NO];
selectedHotspot = (UIButton *)sender;
[selectedHotspot setSelected:YES];
// Get dictionary of hot spot that is pressed
NSDictionary *hotspot = [hotspots objectAtIndex:[selectedHotspot tag]];
NSString *imageFileName = [hotspot objectForKey:ksHotspotItemKey];
if ([imageFileName length] > 0) currentImageView.image = [UIImage imageNamed:imageFileName];
}
}
我遇到的问题是,在用户松开手指之前,按钮的高亮显示图像不会显示,这是明显的延迟。我看到其他人通过更改背景图像而不是按钮状态或在延迟后执行选择器来解决类似问题,因此运行循环有机会结束。这两种方法对我来说都很糟糕,如果有人能够解释这里发生的事情以及实现效果最强大的方式,只要用户触摸按钮就会改变其突出显示的状态,这将是非常感激的。
提前致谢,
戴夫
答案 0 :(得分:1)
找到了解决方法。我为TouchDown创建了一种方法,为TouchUpInside和TouchUpOutside创建了一种方法。 TouchDown只是取消选择按钮(如果已经选中)并更改我的视图图像。 TouchUp事件设置按钮的选定属性。由于突出显示和所选图像都是相同的,因此净效果是按钮在触摸按钮时立即改变,并且在触摸事件之后保持该状态。代码在这里:
// Called when a hotspot is touched down
-(void)hotspotTouchDown:(id)sender{
// Deselect the hotspot currently selected if it exists
if (selectedHotspot) [selectedHotspot setSelected:NO];
// Get dictionary of hot spot that is pressed
NSDictionary *hotspot = [hotspots objectAtIndex:[sender tag]];
// If the class of the hotspot is 'self' then replace the current image with a new one given in the hotspot data
if ([[hotspot objectForKey:ksHotspotClassKey] isEqualToString:ksHotspotClassSelf]) {
NSString *imageFileName = [hotspot objectForKey:ksHotspotItemKey];
if ([imageFileName length] > 0) currentImageView.image = [UIImage imageNamed:imageFileName];
}
}
// Called when a hotspot is touched up
-(void)hotspotTouchUp:(id)sender{
// Set the selected property of the button
selectedHotspot = (UIButton *)sender;
[selectedHotspot setSelected:YES];
}