【问题标题】:UIButton state change does not happen till after touches endUIButton 状态更改直到触摸结束后才会发生
【发布时间】:2011-04-27 14:34:55
【问题描述】:

对不起,如果这是一个基本问题,我找不到明确的答案。

我设置了 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];
    }
}

我遇到的问题是,在用户松开手指之前,按钮的突出显示图像不会显示,这是一个明显的延迟。我看到其他人通过更改背景图像而不是按钮状态或在延迟后执行选择器来解决类似问题,以便运行循环有机会结束。这两种方法对我来说似乎都是 hack,如果有人能解释这里发生了什么以及实现效果的最可靠方法是什么,一旦用户按下按钮,它就会变为突出显示状态,我将不胜感激。

提前致谢,

戴夫

【问题讨论】:

  • 请尝试仅使用 UIControlEventTouchDown 事件。
  • 嗨,Ravin,刚刚尝试仅使用 TouchDown 事件和相同的问题。如果您按下该方法,则调用该方法,因为主图像已更改,但按钮状态不会更改为突出显示,直到您在按钮的矩形中进行 TouchUp 或 DragOutside。

标签: iphone cocoa-touch uibutton


【解决方案1】:

找到了解决办法。我为 TouchDown 创建了一种方法,为 TouchUpInside 和 TouchUpOutside 创建了一种方法。如果按钮已经被选中,TouchDown 会简单地取消选择按钮并更改我的视图图像。 TouchUp 事件设置按钮的 selected 属性。由于突出显示的图像和选定的图像都是相同的,因此最终效果是按钮一旦被触摸就会改变,并且在触摸事件之后保持不变。代码在这里:

// 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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多