【问题标题】:Animate UIButton Background color from bottom up, Objective C动画 UIButton 背景颜色自下而上,Objective C
【发布时间】:2015-10-19 12:42:20
【问题描述】:

我有一个按钮,我想为背景颜色设置动画,就好像它从底部填充到顶部一样。

现在我只是用动画改变颜色

[UIView animateWithDuration:2.0 animations:^{
    button.layer.backgroundColor = [UIColor redColor].CGColor;
} completion:NULL];

这很好用,但我希望动画从下往上填充,就像填充液体一样。

编辑:不确定是否有区别,但按钮是圆形的

【问题讨论】:

  • 你能通过从底部滑入彩色视图来“伪造”它吗?

标签: ios objective-c animation uibutton


【解决方案1】:

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)
}

【讨论】:

    【解决方案2】:

    试试这个。这里 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];
    }];
    

    【讨论】:

    • 工作就像一个魅力,你先生/女士真棒
    • 好的,小问题,转换后按钮不可按下
    • 设置背景颜色后,试试这个 - v.userInteractionEnabled = NO; v.exclusiveTouch = 否;
    【解决方案3】:

    如果您只需要从下到上填充背景颜色,您可以创建具有该颜色和原点 Y = CGRectGetHeight(button.frame) 的附加层。当您需要填充按钮时,只需将该图层的原点 Y 设置为 0。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 2018-10-13
      相关资源
      最近更新 更多