问题描述
|
抱歉,如果这是一个基本问题,我找不到确切的答案。
我设置了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];
}
}
我的问题是,直到用户松开手指,按钮的突出显示的图像才会显示出来,这是一个明显的延迟。我看到其他人通过更改背景图像而不是按钮状态或在延迟后执行选择器来解决类似问题,从而使运行循环有机会结束。这两种方法对我来说似乎都是骇客,如果有人可以解释这里发生了什么以及实现该效果的最可靠方法,即用户一旦按下按钮,它就会变为突出显示的状态,那么这两种方法将不胜感激。
提前致谢,
戴夫
解决方法
找到了解决方法。我为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];
}