【问题标题】:Advice needed on moving UIViews with NSLayoutConstraints使用 NSLayoutConstraints 移动 UIView 所需的建议
【发布时间】:2013-02-05 12:16:27
【问题描述】:

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

为了实现向下滑动,我最终为 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){}];

【问题讨论】:

    标签: ios objective-c nslayoutconstraint


    【解决方案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;
        }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多