要在指定的持续时间内减少视图的不透明度,请将其放入动画块中。您可以设置 alpha - 我个人的偏好是为诸如此类的视觉处理调整图层属性(主要是我的代码库中的一致性问题)。您可以使用此处演示的任何一种方法(不要同时使用两者) - 在这种情况下差异可以忽略不计。如果您修改图层属性,则需要导入 Quart 标头。
- (void)flash:(id)sender
{
UIButton *button = (UIButton *)sender;
NSTimeInterval duration = 1;
[UIView animateWithDuration:duration
animations:^{
// My preferred approach is to set layer opacity...
[[button layer] setOpacity:0.1f];
// But you could just as easily set alpha (per your original question)
[button setAlpha:0.1f];
} completion:^(BOOL finished) {
// Returns the button to full opacity on completion.
[[button layer] setOpacity:1.0f];
// Or alternately, alpha
[button setAlpha:1.0f];
}];
}