iOS Change alpha of UIButton when there is an EventTouchDown

别等时光非礼了梦想. 提交于 2020-01-15 03:34:10

问题


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

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

Thanks you!!!


回答1:


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

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



来源:https://stackoverflow.com/questions/8702216/ios-change-alpha-of-uibutton-when-there-is-an-eventtouchdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!