xcode 当有 EventTouchDown 时,iOS 更改 UIButton 的 alpha

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8702216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:52:17  来源:igfitidea点击:

iOS Change alpha of UIButton when there is an EventTouchDown

objective-ciosxcodeuikit

提问by KevinM

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:

我想在 UIButton 上有 EventTouchDown 时更改它的 alpha。我希望有一种类似于以下代码的方法:

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

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

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

Thanks you!!!

谢谢!!!

回答by Ignacio Inglese

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.

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

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

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

如果您需要在 selected 上执行某些操作,您可以执行与此处相同的操作