问题
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