动画ui按钮背景颜色从下向上,目标C



我有一个按钮,我想把它的背景颜色动画化,就好像它是从下到上填充的。

现在我只是用动画

改变颜色

。e

[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。

最新更新