使用约束移动视图

Bro*_*sef 4 objective-c ios autolayout nslayoutconstraint swift

我的视图控制器中有几个视图,当检测到向下滑动时向下移动,然后在检测到向下滑动时向下移动.我通过使用CGRectOffset调整y原点来强制移动视图.我现在已经对IB的观点应用了约束,我不确定最好的方法是移动视图,以便它们最终在iphone 5,6和6+上的正确位置.

目前我正在做这样的事情:

[self.view layoutIfNeeded];

    self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant +338;

    [UIView animateWithDuration:5
                     animations:^{
                         [self.view layoutIfNeeded];
                     }];
Run Code Online (Sandbox Code Playgroud)

使用比率更改常量是否更好?因此,对于上面的约束,而不是使用338,这样做会更好:

    self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant + (self.panView.frame.size.height/1.680);

    //self.panView.frame.size.height = 568
    //(568/1.680) = 338
Run Code Online (Sandbox Code Playgroud)

Kam*_*pai 23

是的,更改常量时没有问题.这里的事情是你必须适当地设置你的约束.

让我们考虑一个例子.

我有一个UIView故事板,我想改变它的宽度.它的默认宽度是1024.

在一些动画之后,我们将它的宽度改为900.

按照以下步骤实现此目的:

  1. 选择UIView我们要更新约束的位置.这里我们需要更新width,所以我们将为width添加一个约束.

在此输入图像描述

  1. 现在我们可以看到添加了新的约束UIView.

在此输入图像描述

  1. 单击该约束.它将显示约束属性.现在我们要将宽度从1024减小到900,这样在约束属性中将Relation属性更改为Less Than或Equal.同样,如果你想从1024增加宽度,那么Relation大于或等于.

在此输入图像描述

  1. 现在IBOutletNSLayoutConstraint上面的宽度约束创建一个变量并将其连接起来.

变量看起来像:

在目标C中:

@IBOutlet weak var viewWidthConstraint : NSLayoutConstraint!

func reduceWidth() {
    // Reduce width of view.
    UIView.animate(withDuration: 0.35, animations: { () -> Void in
        self.viewWidthConstraint.constant = 900
        self.view.layoutIfNeeded()
    })
}

func normalWidth() {
    // Change to the default width of view.
    UIView.animate(withDuration: 0.35, animations: { () -> Void in
        self.viewWidthConstraint.constant = 1024
        self.view.layoutIfNeeded()
    })
}
Run Code Online (Sandbox Code Playgroud)

在Swift中:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint     *viewWidthConstraint; 

- (void) reduceWidth {
    // Reduce width of view.
    [UIView animateWithDuration:0.35f animations:^{
        self.viewWidthConstraint.constant = 900;
        [self.view layoutIfNeeded];
    }];
}

- (void) normalWidth {
    // Change to default width of view.
    [UIView animateWithDuration:0.35f animations:^{
        self.viewWidthConstraint.constant = 1024;
        [self.view layoutIfNeeded];
    }];
}
Run Code Online (Sandbox Code Playgroud)
  1. 改变宽度常数:

使用以下代码减少常量:

目标C:

@IBOutlet weak var viewWidthConstraint : NSLayoutConstraint!

func reduceWidth() {
    // Reduce width of view.
    UIView.animate(withDuration: 0.35, animations: { () -> Void in
        self.viewWidthConstraint.constant = 900
        self.view.layoutIfNeeded()
    })
}

func normalWidth() {
    // Change to the default width of view.
    UIView.animate(withDuration: 0.35, animations: { () -> Void in
        self.viewWidthConstraint.constant = 1024
        self.view.layoutIfNeeded()
    })
}
Run Code Online (Sandbox Code Playgroud)

Swift 4.0:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint     *viewWidthConstraint; 

- (void) reduceWidth {
    // Reduce width of view.
    [UIView animateWithDuration:0.35f animations:^{
        self.viewWidthConstraint.constant = 900;
        [self.view layoutIfNeeded];
    }];
}

- (void) normalWidth {
    // Change to default width of view.
    [UIView animateWithDuration:0.35f animations:^{
        self.viewWidthConstraint.constant = 1024;
        [self.view layoutIfNeeded];
    }];
}
Run Code Online (Sandbox Code Playgroud)

同样我们可以将其更改为默认值:

目标C:

@IBOutlet weak var viewWidthConstraint : NSLayoutConstraint!

func reduceWidth() {
    // Reduce width of view.
    UIView.animate(withDuration: 0.35, animations: { () -> Void in
        self.viewWidthConstraint.constant = 900
        self.view.layoutIfNeeded()
    })
}

func normalWidth() {
    // Change to the default width of view.
    UIView.animate(withDuration: 0.35, animations: { () -> Void in
        self.viewWidthConstraint.constant = 1024
        self.view.layoutIfNeeded()
    })
}
Run Code Online (Sandbox Code Playgroud)

Swift 4.0:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint     *viewWidthConstraint; 

- (void) reduceWidth {
    // Reduce width of view.
    [UIView animateWithDuration:0.35f animations:^{
        self.viewWidthConstraint.constant = 900;
        [self.view layoutIfNeeded];
    }];
}

- (void) normalWidth {
    // Change to default width of view.
    [UIView animateWithDuration:0.35f animations:^{
        self.viewWidthConstraint.constant = 1024;
        [self.view layoutIfNeeded];
    }];
}
Run Code Online (Sandbox Code Playgroud)

  • 伟大而正确的答案.我只想补充一下,设置约束不需要在`UIView.animateWithDuration`块中完成.您可以在动画块之前设置约束,只需在块内调用`layoutIfNeeded`. (2认同)