微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UIButton的状态更改直到触摸结束后才发生

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

解决方法

找到了解决方法。我为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];
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?