Fading out an UIButton when touched

前端 未结 1 1796
遇见更好的自我
遇见更好的自我 2020-12-17 06:34

I\'ve got a Selected-state and a Normal-state for an UIButton that both are UIImages. When a button is touched, I\'d like it to hit the selected-state and then animate back

相关标签:
1条回答
  • 2020-12-17 07:05

    Since selected is not an animatable property, that won't work (as you've found out). My solution would be to have the selected state of the btn be in a separate UIImageView directly below the button in the exact same location. Then in the action for tapping the button:

    - (void) tapButton:(UIButton *)btn {
        btn.alpha = 0;
        [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationDelegate:[UIApplication sharedApplication]];
        [UIView setAnimationDidStopSelector:@selector(endIgnoringInteractionEvents)];
        btn.alpha = 1;
        [UIView commitAnimations];
    }
    

    Note I also added the begin/endIgnoringInteractionEvents calls so the user can't tap on the button while it's fading back to its normal state. If you want to allow that, replace the begin/end calls with [UIView setAnimationBeginsFromCurrentState];

    0 讨论(0)
提交回复
热议问题