如何以编程方式突出显示UIButton? [英] How to programmatically highlight a UIButton?

查看:53
本文介绍了如何以编程方式突出显示UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多用户抱怨iPhone上的小"i"信息按钮很难触摸.好的,很简单-我只是在它后面贴了一个大脂肪的不可见按钮,即使最草率的手指触摸,您也不会错过;当您触摸它时,它会执行infoButtonAction.

I've had a lot of users complain that the little "i" info button is difficult to touch on the iPhone. Ok, simple enough -- I just stuck a big-fat invisible button behind it that you can't miss even with the sloppiest of finger touches and, when you touch it, it does the infoButtonAction.

问题是,我想闪烁信息"按钮本身约.25秒,以给出视觉这是怎么回事"类型的反馈.我的意思是,我已经假设您要按下"i"按钮,所以我只是将其视为您 DID 按下了它.

Thing is, I'd like to flash the info button, itself, for about .25 sec, just to give a visual "this is what's going on" type of feedback. I mean, I'm already assuming that you meant to hit the "i" button, so I'm just treating it as if you DID hit it.

我尝试了此操作,但没有用:

I tried this, but it doesn't work:

    UIImage* normalImage = [_infoButton imageForState:UIControlStateNormal];
    UIImage* highlighted = [_infoButton imageForState:UIControlStateHighlighted];
    _infoButton.highlighted = YES;                                          // flash the button
    [_infoButton setImage:highlighted forState:UIControlStateNormal];
    [_infoButton setNeedsDisplay];                                          //* FIXME: No flash?!

    [(AppDelegate*)[[UIApplication sharedApplication] delegate] infoTap];   // do the info action

    _infoButton.highlighted = NO;   
    [_infoButton setImage:normalImage forState:UIControlStateNormal];
    [_infoButton setNeedsDisplay];

关于如何获得我想要的行为的任何想法吗?

Any ideas about how to get the behaviour I want?

(我也乐于接受有关用户反馈的其他想法,但仍然很好奇我会怎么做.想象一下,相反,我有一个游戏"/恶作剧,您可以在其中按下确定"按钮并取消"闪烁,反之亦然,或者同样愚蠢的东西.)

(I'm also open to alternate ideas about user feedback, but still curious how I'd do this. Imagine that, instead, I have a "game"/prank where you push the "ok" button and "cancel" flashes, and vice versa, or something equally silly.)

推荐答案

问题是您将按钮标记为需要显示(无需调用 -setNeedsDisplay ;按钮在内部进行调用),但是永远不要让运行循环有机会显示新图像.

The problem is that you are marking your button as needing display (calling -setNeedsDisplay is unecessary; the button calls that internally), but then never allowing the run loop a chance to display the new image.

在Cocoa中,您可以使用类似 -performClick:的功能,但这在iPhone上不可用.

In Cocoa, you could use something like -performClick: but that is not available on the iPhone.

相反,我建议您尝试在之间延迟的情况下调用 -setSelected: -setHighlighted:.

Instead, I suggest you experiment with calling -setSelected: or -setHighlighted: with a delay in between.

这篇关于如何以编程方式突出显示UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆