我正在练习自动布局和学习动画约束。iOS - 限制在viewdidload中以意想不到的方式动画?
我的第一个问题是。如果我动态地添加视图,将它们的约束动态添加到父视图似乎也很麻烦。有没有任何干净的方式来实现灵活的布局,可以通过编程方式添加和删除视图?或者这意味着我应该想出一个更简单的解决方案来实现我想要完成的任务?
第二个问题。我创建了两个视图,还有一些代码约束。我只是试图调整负载上的第一个视图的高度约束,以便它变得更短,第二个视图将相应地向上移动。
这里是一些代码:
first = [[UIView alloc]initWithFrame:CGRectZero];
[first setBackgroundColor:[UIColor blueColor]];
[first setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:first];
UIView *second = [[UIView alloc]initWithFrame:CGRectZero];
[second setBackgroundColor:[UIColor redColor]];
[second setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:second];
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:first attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:20];
NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:first attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-20];
top = [NSLayoutConstraint constraintWithItem:first attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:40];
height = [NSLayoutConstraint constraintWithItem:first attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:80];
[self.view addConstraints:@[leading,trailing,top,height]];
[height setConstant:10];
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
好了,所以在这里的底部我跑我的动画..我的第二个观点是已经定位哪里会在动画的结尾。第一个视图从左上角扩展到右下角。它对角动画并以10的高度结束。
任何人都可以解释这种行为。我注意到,如果我分配约束,并使它们在IBAction上有动画(按钮触摸),那么它将按预期动画。
感谢我想我需要发挥它多一点 – hamobi
我想我的问题是..如果IM添加其他视图之间的意见..想,如果我有看法 一个 乙 Ç ,然后我想在他们之间的这种方式来增加随机视图X 一个 乙 X ç 然后我要更新视图B和C,的限制使用与创建十限制,这似乎真的很麻烦 – hamobi
感谢沿更新的信息。欣赏它 – hamobi