ios – 自动布局:动画更改为NSLayoutConstraint的乘数

前端之家收集整理的这篇文章主要介绍了ios – 自动布局:动画更改为NSLayoutConstraint的乘数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个简单的观点:
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. redBox = [[UIView alloc] init];
  5. [redBox setBackgroundColor:[UIColor redColor]];
  6. [redBox setTranslatesAutoresizingMaskIntoConstraints:NO];
  7. [self.view addSubview:redBox];
  8.  
  9. //H:
  10. widthConstraint = [NSLayoutConstraint constraintWithItem:redBox
  11. attribute:NSLayoutAttributeWidth
  12. relatedBy:NSLayoutRelationEqual
  13. toItem:self.view
  14. attribute:NSLayoutAttributeWidth
  15. multiplier:0.5
  16. constant:0.0];
  17. [self.view addConstraint:widthConstraint];
  18.  
  19. //More constraints...
  20. }

我想动画一下redBox宽度的增加.

我试过这个:

  1. widthConstraint.constant = 100;
  2.  
  3. [UIView animateWithDuration:1
  4. animations:^{
  5. [self.view layoutIfNeeded];
  6. }
  7. ];

这会将视图的宽度增加100并对其进行动画处理,但我需要将其增加一个与其超视图宽度成比例的量.换句话说,我想将widthConstraint的乘数设置为0.8.

但是NSLayoutConstraint’s multiplier is readonly!这是否意味着每次我想要对其乘数进行动画处理时,我必须删除,修改和重新添加widthConstraint,还是有更好的方法

解决方法

不幸的是,没有更好的方法.使用不同的乘数删除和重新添加是您唯一的选择.请提交一个您希望看到多重读取的错误.

猜你在找的iOS相关文章