当有 EventTouchDown 时,iOS 更改 UIButton 的 alpha [英] iOS Change alpha of UIButton when there is an EventTouchDown

查看:24
本文介绍了当有 EventTouchDown 时,iOS 更改 UIButton 的 alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 UIButton 有 EventTouchDown 时,我想改变它的 alpha.我希望有一种类似于以下代码的方法:

I would like the change the alpha of a UIButton when there is an EventTouchDown on it. I'm was hoping there was a way to do it similar to the below code:

[self.myButton setImage:<#(UIImage *)#> forState:UIControlEventTouchDown];

有没有办法用setAlpha"做这样的事情?

Is there a way to do something like this with "setAlpha"?

谢谢!!!

推荐答案

您可以对 UIButton 进行子类化(不推荐,但由于您最终要做的只是覆盖 setHighlighted 或 setSelected,所以这里应该不是什么大问题) 并覆盖点击方法来处理您的 alpha 变化.

You could subclass your UIButton (not recommended, but since all you'd end up doing is overriding setHighlighted or setSelected it shouldn't be much of an issue here) and override the tapping methods to handle your alpha change.

// In your UIButton's subclass
- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    if(self.highlighted) {
        [self setAlpha:0.5];
    }
    else {
        [self setAlpha:1.0];
    }
}

如果你需要在 selected 上做一些事情,你可以在这里做同样的事情

If you need to do something on selected you could do the same as in here

这篇关于当有 EventTouchDown 时,iOS 更改 UIButton 的 alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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