[英]Animate Background Color Of UIButton from WhiteColor to RedColor
[英]Animate UIButton Background color from bottom up, Objective C
我有一个按钮,希望对背景颜色进行动画处理,好像它是从底部填充到顶部一样。
现在我只是用动画改变颜色
即
[UIView animateWithDuration:2.0 animations:^{
button.layer.backgroundColor = [UIColor redColor].CGColor;
} completion:NULL];
效果很好,但我希望动画从下往上填充,就像它充满液体一样。
编辑:不确定是否有所作为,但按钮是圆形的
尝试这个。 这里btn是按钮的出口,v是我们为了动画目的将添加到按钮中的视图。
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, [self btn].frame.size.height,
[self btn].frame.size.width, [self btn].frame.size.height)];
[v setBackgroundColor:[UIColor purpleColor]];
v.userInteractionEnabled = NO;
v.exclusiveTouch = NO;
[[self btn] addSubview:v];
[UIView animateWithDuration:0.45 animations:^{
CGRect currentRect = v.frame;
currentRect.origin.y = 0;
[v setAlpha:1];
[v setFrame:currentRect];
[[self btn] sendSubviewToBack:v];
}];
Swift
版本的Avi答案。
let v = UIView(frame: CGRect(x: 0, y: self.btn.frame.size.height,
width: self.btn.frame.size.width,
height: self.btn.frame.size.height))
v.backgroundColor = .purple
v.isUserInteractionEnabled = false
v.isExclusiveTouch = false
self.btn.addSubview(v)
UIView.animate(withDuration: 0.45) {
var currentRect = v.frame
currentRect.origin.y = 0
v.alpha = 1.0
v.frame = currentRect
self.btn.sendSubview(toBack: v)
}
如果只需要从底部到顶部填充背景色,则可以使用该颜色和原点Y = CGRectGetHeight(button.frame)创建附加层。 当您需要填充按钮时,只需将该图层的原点Y动画为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.