ios - 动画约束变化,位置动画但高度跳跃没有动画

标签 ios ios7 nslayoutconstraint

我有一个自定义的 UIView。在那个 UIView 中,我有一个带有黄色背景的 UILabel,其中包含名为 labelWhite 的 @"8"。我以编程方式为该 UILabel 创建了 4 个约束:顶部、左侧、宽度和高度。

当我点击 UIView 时,我会更改高度的常量值并在 5 秒内为布局设置动画。然而, View 的高度立即跳转到新值,但 y 位置也发生变化并立即跳转到新值。然后在 5 秒的动画中,y 位置动画回到它应该一直停留的位置。

您可以在此处观看相关视频:http://inadaydevelopment.com/stackoverflow/Constraints0.mov

它应该做的只是保持在 y=0 并垂直收缩。我做错了什么?


编辑:

我刚刚发现,如果我的 subview 是 UIView,我的动画会完全按照预期工作,但作为 UILabel,我得到了 jump-size + animate-position。

这是怎么回事?有没有办法让 UILabels 以动画方式显示它们的大小?


这是我用来修改和动 Canvas 局的代码:

self.constraintWhiteHeight.constant = secondaryHeight;

[UIView animateWithDuration:5.0 animations:^{
    [self layoutIfNeeded];
}];

这是我用来创建约束的代码:

// ------ Top -----
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
                                                              attribute:NSLayoutAttributeTop
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.labelWhite
                                                              attribute:NSLayoutAttributeTop
                                                             multiplier:1.0
                                                               constant:0];
[self addConstraint:constraint];

// ------ Left -----
self.constraintWhiteLeft = [NSLayoutConstraint constraintWithItem:self
                                                        attribute:NSLayoutAttributeLeft
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:self.labelWhite
                                                        attribute:NSLayoutAttributeLeft
                                                       multiplier:1.0
                                                         constant:0];

// ------ Width -----
NSString *constraintString = [NSString stringWithFormat:@"H:[_labelWhite(==%.0f)]", self.bounds.size.width];
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraintString
                                                               options:0
                                                               metrics:nil
                                                                 views:NSDictionaryOfVariableBindings(_labelWhite)];
NSLog(@"constraints: %@", constraints);
self.constraintWhiteWidth = [constraints objectAtIndex:0];
[self.labelWhite addConstraints:constraints];

// ------ Height -----
constraintString = [NSString stringWithFormat:@"V:[_labelWhite(==%.0f)]", primaryHeight];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraintString
                                                      options:0
                                                      metrics:nil
                                                        views:NSDictionaryOfVariableBindings(_labelWhite)];
self.constraintWhiteHeight = [constraints objectAtIndex:0];
[self.labelWhite addConstraints:constraints];

最佳答案

我不认为您做错了什么,这似乎是标签在您尝试设置其高度动画时的行为方式。我知道我过去曾为这个问题苦苦挣扎过,我不记得我是否曾经找到过通过在动画 block 中为约束设置动画来工作的解决方案。我让它工作的方法是使用计时器或 CADisplayLink 来调整高度约束。

-(void)shrinkLabel {
    self.constraintWhiteHeight.constant -= 1;
    if (self.constraintWhiteHeight.constant >= secondaryHeight)
    [self performSelector:@selector(shrinkLabel) withObject:nil afterDelay:.05];
}

编辑后:

虽然计时器方法有效,但它并不总是看起来很流畅,具体取决于您使用的速度和增量。另一种方法是使用一个大的 UIView(与电影中的黄色标签大小相同),里面有一个较小的 UILabel,它对较大的 View 具有 centerX 和 centerY 约束。然后,像往常一样使用 animateWithDuration:animations: 为黄色 View 设置动画:

enter image description here

-(void)shrinkLabel {
    self.yellowViewHeightCon.constant = secondaryHeight;
    [UIView animateWithDuration:5 animations:^{
        [self layoutIfNeeded];
    }];
}

关于ios - 动画约束变化,位置动画但高度跳跃没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23751851/

相关文章:

ios - 分组表中的 TableView 页眉/页脚字体(Swift)

ios - UIScrollView 只能用一根手指滚动

swift - 通过更改约束常量来动画 View

swift - 设置 `constraint.isActive = false` 是否解除约束?

ios - 需要有关以编程方式创建自动布局约束的帮助

ios - IBM Watson 语音转文本 : Not able to transcribe the text using the Swift SDK

ios - Q : tableView created by storyboard running appears space form the top to the first cell

ios - 在没有互联网的情况下将消息从服​​务器发送到 iPad

ios - 在 iOS 7 Mobile Safari 的书签中,Apple 如何让导航 Controller 推送时分段控件持久化?

ios - 当图像及其按钮旋转时,相机应用程序如何防止其工具栏旋转?