0

从下面的图片中,当用户点击 view1 时,我让 view2 和 view3 向下滑动到页脚,通过将 view3 的约束常量与页脚设置为 0(ish)来有效地拉动。我的 xib 设置了约束,如第一张图片所示。其中最重要的 2 个 [对我来说现在] 是 view1View2 约束和 view3Footer 约束

带有约束和视图的 xib

为了实现向下滑动,我最终为 view1view2 约束设置了低优先级,为 view3Footer 约束设置了更高的优先级,然后在 animateWithDuration 中更新 view3Footer 约束常量

成功滑下

我的问题是让 view2 和 view3 向后滑动,如果我使用相同的方法,我可以通过将 view1view2 约束常量设置为 2 来实现。

我相信上面上滑的问题是 view3Footer 约束比 view1View2 约束具有更高的优先级,优先级似乎只是只读的,所以我不能专门更改这些。我知道在设置约束时我只要求视图定位。

...我相信我可能完全使用了错误的方法...

我是否以正确的方式解决这个问题?我是否必须获取约束对象 IBOutlet 并重写它们?如果是这样,我会重写优先级吗?我是否应该将 >= 用于具有相同优先级的常量,这似乎不起作用。下面是我的简单动画代码,虽然不多,但除了手势识别器之外,设置主要在 xib

很感谢任何形式的帮助。谢谢,史蒂夫

对于向下滑动:

[UIView animateWithDuration:0.9 animations:^{
                        _view3FooterConstraint.constant=2;
                        [self.view layoutIfNeeded];
                        } completion:^(BOOL finished){}];

UPDATE 也试过这个设置优先级相等——不能再实现向下滑动

_view3FooterConstraint.constant=2;
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.9 animations:^{                            
                        [self.view layoutIfNeeded];
                        } completion:^(BOOL finished){}];

对于上滑:

[UIView animateWithDuration:0.9 animations:^{
    _view1View2Constraint.constant=2;
    [self.view layoutIfNeeded];
} completion:^(BOOL finished){}];
4

1 回答 1

1

我想我会通过添加和删除更改的 2 个约束来做到这一点(2 的顶部约束为 1,3 的底部约束为页脚)。确保所有视图都有明确的高度,并使 IBOutlets 符合我上面提到的 2 个约束。您实际上一次只需要其中一个,但您需要在 IB 中添加这两个,以便您可以为它们创建出口。在 viewDidLoad 中,我立即删除了底部的那个。这应该适用于纵向和横向。我使用的代码是:

implementation ViewController {
   IBOutlet NSLayoutConstraint *conBottom;
   IBOutlet NSLayoutConstraint *conTop;
    int pos;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    pos = 0;
    [self.view removeConstraint:conBottom];
    [self.view layoutSubviews];
}


-(IBAction)togglePosition:(id)sender { //tap recognizer on view one action method
    if (pos == 0) {
        [self moveDown];
    }else{
        [self moveUp];
    }
}

-(void)moveDown {
    [self.view removeConstraint:conTop];
    [self.view addConstraint:conBottom];
    conBottom.constant=2;
    [UIView animateWithDuration:0.9 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        pos = 1;
    }];
}

-(void)moveUp {
    [self.view removeConstraint:conBottom];
    [self.view addConstraint:conTop];
    conTop.constant=2;
    [UIView animateWithDuration:0.9 animations:^{
        [self.view layoutIfNeeded];
    }completion:^(BOOL finished) {
        pos = 0;
    }];
}
于 2013-02-06T07:24:26.920 回答