2011-04-14 87 views
1

对不起,如果这是一个基本问题,我找不到一个明确的答案。UIButton状态改变不会发生,直到接触结束

我已经设置了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

请仅尝试使用UIControlEventTouchDown事件。 – Ravin 2011-04-14 11:00:33

+0

嗨Ravin,刚试过只TouchDown事件和相同的问题。如果触地,方法会被调用,因为主图像被改变,但是按钮状态不会改变为高亮显示,直到您按下TouchUp或DragOutside按钮的矩形。 – 2011-04-14 15:58:07

回答

1

有没有发现周围的工作。我为TouchDown创建了一个方法,为TouchUpInside和TouchUpOutside创建了一个方法。如果TouchDown已被选中并且改变我的视图的图像,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]; 
} 
相关问题