问题描述
我有一个UIButton的选择状态和正常状态,这两个都是UIImages。当一个按钮被触摸时,我想让它达到选择状态,然后在一秒钟的时间内动画回到正常状态。我按下UIButton * btn时设置了以下动画,但它只是再次切换到取消选择的状态。我应该如何实现这一目标?
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 to the normal-state over the period of one second. I've set the following animation when the UIButton* btn is pressed, but it just switches right back to deselected state again. How should I go about achieving this?
[btn setSelected:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0f];
[btn setSelected:NO];
[UIView commitAnimations];
干杯
Nik
推荐答案
由于选择
不是一个动画属性,发现了)。我的解决方案是使btn的选定状态在单独的UIImageView直接下面的按钮在完全相同的位置。然后在点击按钮的操作中:
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];
}
注意我也添加了 begin / endIgnoringInteractionEvents
调用,因此用户在退回到其正常状态时无法点击按钮。如果你想允许,用
[UIView setAnimationBeginsFromCurrentState];
这篇关于触摸时淡出UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!