5

所以我正在尝试为一些布局约束设置动画并且运气不佳,我只是什么也没看到,然后我收到一条错误消息,说明它如何不能同时满足所有约束等。

我正在使用一个名为PullableView的类,它的动画使用旧样式[UIView commitAnimation],所以我将它子类化并添加到我的代码中,我认为这将用于动画约束......没有这样的运气并试图对其进行动画处理,甚至让它做很多事情任何事情都被证明是困难的,我只是得到“无法同时满足约束”。问题是我对这个约束业务相当陌生,所以我不知道从哪里开始。

这是错误,另一个几乎相同,但对于centerY。

"<NSLayoutConstraint:0x7c8a3a0 StyledPullableView:0x905a9b0.centerX == UIView:0x9054680.centerX>", "<NSLayoutConstraint:0x7c6b480 StyledPullableView:0x905a9b0.centerX == UIView:0x9054680.centerX + 128>"

我当然会[pullRightView setTranslatesAutoresizingMaskIntoConstraints:NO];在调用这个之前

任何帮助表示赞赏。

- (void)setOpenedForConstraints:(BOOL)op animated:(BOOL)anim
{
    opened = op;

    if (anim)
    {        
        NSLayoutConstraint *constraintX = [NSLayoutConstraint constraintWithItem:self
                                                                               attribute:NSLayoutAttributeCenterX
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:_parentView
                                                                               attribute:NSLayoutAttributeCenterX
                                                                              multiplier:1
                                                                                constant:0];

        NSLayoutConstraint *constraintY = [NSLayoutConstraint constraintWithItem:self
                                                                               attribute:NSLayoutAttributeCenterY
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:_parentView
                                                                               attribute:NSLayoutAttributeCenterY
                                                                              multiplier:1
                                                                                constant:0];

        [_parentView addConstraint:constraintX];
        [_parentView addConstraint:constraintY];

        constraintX.constant = opened ? self.openedCenter.x : self.closedCenter.x;
        constraintY.constant = opened ? self.openedCenter.y : self.closedCenter.y;
    }

    if (anim)
    {

        // For the duration of the animation, no further interaction with the view is permitted
        dragRecognizer.enabled = NO;
        tapRecognizer.enabled = NO;

        //[UIView commitAnimations];

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

    }
    else
    {

        if ([delegate respondsToSelector:@selector(pullableView:didChangeState:)])
        {
            [delegate pullableView:self didChangeState:opened];
        }
    }
}
4

1 回答 1

11

几个想法:

  1. 你在这里创建了新的约束。通常,您将调整constant现有约束的 。或者,如有必要,您将删除旧约束,并添加一个新约束。

  2. 更改现有约束时,有两种方法。一种是遍历视图的约束,找到有问题的约束,然后constant为此调整。另一种方法(当视图是您在 Interface Builder 中添加的视图时更容易)是在 Interface Builder 中IBOutlet为约束权创建一个。然后你可以为它制作动画。

    • 这个答案在关注另一个问题的同时,说明了IBOutlet为约束创建一个然后动画其值变化的过程。

    • 如果您必须遍历约束以查找约束(例如,从您可能使用可视格式语言创建的整个集合中挑选一个约束),那么此答案replaceLeadingAndTopWithCenterConstraints中的说明可能是一个有用的示例,说明您如何找到现有视图的约束。这个特殊的例子是删除一个约束并添加一个新的约束,但是你可以很容易地,一旦你找到现有的约束,调整它的常数。


我不完全确定您要使用的 UX,但假设您有一个面板要滑到屏幕左侧:

  1. 第一个问题是你定义了什么约束:在这个例子中,我定义了四个约束,三个到超级视图(即顶部、底部和左侧,但不是右侧)和一个宽度约束。如果您在 IB 中执行此操作,它可能会很挑剔(例如,在添加宽度约束之前,您无法删除正确的约束)。只需确保约束已完全定义,但至少应如此。

  2. constant然后我可以通过更改左侧约束来动画面板滑出屏幕:

    - (IBAction)touchUpInsideButton:(id)sender
    {
        self.offscreen = !self.offscreen;
    
        [UIView animateWithDuration:0.5
                         animations:^{
                             self.panelLeftConstraint.constant = ([self isOffscreen] ? -kHowMuchToSlideOffToLeft : 0.0);
                             [self.view layoutIfNeeded];
                         }];
    }
    
  3. 您现在可以看到为什么确保没有定义正确的约束很重要。因为如果我在定义了宽度约束右约束的同时调整了左约束,那么它将变得无法满足。通过确保在开始时对约束进行了最低限度的定义(尽管明确定义),我可以更改左侧约束,并且视图现在可以按照我想要的方式进行动画处理。

于 2013-05-21T04:30:44.713 回答