Ios 从下至上为UIButton背景色设置动画,目标C
我有一个按钮,我想动画的背景色,好像它是填补从底部到顶部 现在我只是用动画改变颜色 i、 e 这一切都很好,但我希望动画能像充满液体一样从下往上填充Ios 从下至上为UIButton背景色设置动画,目标C,ios,objective-c,animation,uibutton,Ios,Objective C,Animation,Uibutton,我有一个按钮,我想动画的背景色,好像它是填补从底部到顶部 现在我只是用动画改变颜色 i、 e 这一切都很好,但我希望动画能像充满液体一样从下往上填充 编辑:不确定它是否有区别,但按钮是圆形的如果您只需要从下到上填充背景色,您可以使用该颜色和原点Y=CGRectGetHeight(button.frame)创建附加层。当您需要填充按钮时,只需将该层的原点Y设置为0即可。试试这个。这里btn是按钮的出口,v是我们将添加到按钮中用于动画目的的视图 UIView *v = [[UIView alloc]
编辑:不确定它是否有区别,但按钮是圆形的如果您只需要从下到上填充背景色,您可以使用该颜色和原点Y=CGRectGetHeight(button.frame)创建附加层。当您需要填充按钮时,只需将该层的原点Y设置为0即可。试试这个。这里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
答案版本
你能通过从底部插入彩色视图来“假装”吗?像一个魔咒一样工作,你的先生/女士很了不起好的,轻微的问题,按钮在转换后无法按下设置背景色后,尝试这个-v.userInteractionEnabled=NO;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];
}];
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)
}