如果项目中控件的约束用的是masonry,现在希望实现点击按钮渐变出现控件,那么方法有很多种,其中通过UIView的Animation方法是可以实现的.但是这里会有一个小坑,可能会造成出现的动画效果和预期的不一样.原因在于所需要动画控件的容器里面内容的多少.
// 1.实例化控件并通过masonry设置约束
UILabel *label = [UILabel createOneLabel:CGRectZero title:@"test" superView:self.view fontSize:13 textColor:[UIColor redColor]];
self.label = label;
label.backgroundColor = [UIColor grayColor];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.equalTo(self.view);
make.height.equalTo(@30);
make.top.equalTo(self.view).offset(400);
}];
// 2.点击按钮的方法中实现动画效果
[UIView animateWithDuration:2 animations:^{
if (btn.selected == YES) {
self.label.height = 100;
}else{
// 和文本内容有关
// 如果容器内部内容所需高度大于height 那么就是所需要的渐变
// 如果容器没有内容,那么渐变的效果就突兀了
self.label.height = 15;
}
}];
-
这是label没有文字显示出来的效果,当动画中height改为0就不会有这种突兀了
当label中有文字,则根据文字具体大小,动画效果是不同的.