簡體   English   中英

從下至上對UIButton背景顏色進行動畫處理,目標C

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM