移动与约束意见 [英] Moving views with constraints

查看:140
本文介绍了移动与约束意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的视图控制器一对夫妇的意见当检测到了刷卡再往下检测向下滑动时向上移动。我被强迫的意见通过调整使用CGRectOffset Y原点移动。现在我申请的限制我与IB的看法和我不知道什么让他们在iPhone 5,6和6 +正确的位置结束了最好的方式来移动意见。

目前我在做这样的事情:

  [self.view layoutIfNeeded]    self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant +338;    [UIView的animateWithDuration:5
                     动画:^ {
                         [self.view layoutIfNeeded]
                     }];

是更好地更改使用比例常数?因此,对于上述的约束,而不是使用338,那会是更好地做到这一点:

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


解决方案

是的,当你改变常量没有问题。这里的东西是物质,你必须适当地设置限制。

让我们来看一个例子。

我有一个的UIView 在故事板,我想改变它的宽度。它的deafult宽度 1024

在一些动画,我想改变它的宽度 900

以下几点我也要跟着来实现:


  1. 选择的UIView 中,我们要更新的限制。在这里,我们需要更新宽度,所以我们将增加约束的宽度。

<醇开始=2>
  • 现在,我们可以看到新的约束添加到的UIView

    <醇开始=3>
  • 单击上的约束。它会显示约束属性。在这里,现在我们要减少宽度从1024到900,所以在约束属性变化的关联属性的小于或等于。同样,如果你需要增加从默认宽度和关联大于或等于

  • <醇开始=4>
  • 现在,创建 NSLayoutConstraint IBOutlet中变量,并将其与上述宽度约束连接。

  • 变量如下:

    目标C code:

      @property(弱,非原子)IBOutlet中NSLayoutConstraint * viewWidthConstraint;

    雨燕code:

      @IBOutlet弱VAR viewWidthConstraint:NSLayoutConstraint!


  • 因为我们需要code键更改常数:

  • 要不断降低我们使用以下code:

    目标C code:

      //减少的观点宽度。
    [UIView的animateWithDuration:0.35f动画:^ {
        self.viewWidthConstraint.constant = 900;
        [self.view layoutIfNeeded]
    }];

    雨燕code:

      //减少的观点宽度。
    UIView.animateWithDuration(0.35,动画:{() - GT;无效的
        self.viewWidthConstraint.constant = 900
        self.view.layoutIfNeeded()
    })

    和同样,我们可以将其更改为默认设置:

    目标C code:

      //切换到视图默认宽度。
    [UIView的animateWithDuration:0.35f动画:^ {
        self.viewWidthConstraint.constant = 1024;
        [self.view layoutIfNeeded]
    }];

    雨燕code:

      //切换到视图默认宽度。
    UIView.animateWithDuration(0.35,动画:{() - GT;无效的
        self.viewWidthConstraint.constant = 1024
        self.view.layoutIfNeeded()
    })

    I have a couple views in my view controller that move up when an up swipe is detected then down when a down swipe is detected. I was forcing the views to move by adjusting the y origin using CGRectOffset. I've now applied constraints to my views with IB and I'm not sure whats the best way to move the views so that they end up in the right position on the iphone 5, 6, and 6+.

    Currently I'm doing something like this:

    [self.view layoutIfNeeded];
    
        self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant +338;
    
        [UIView animateWithDuration:5
                         animations:^{
                             [self.view layoutIfNeeded];
                         }];
    

    Is it better to change the constants using ratios? So for the constraint above, instead of using 338, would it be better to do this:

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

    解决方案

    Yes no issue when you change the constants. The thing here is matter that you have to set your constraint appropriately.

    Let's consider an example.

    I have a UIView in Storyboard and I would like to change its width. It's deafult width is 1024.

    After some animation I would like to change it's width to 900.

    Following points I have to follow to achieve this:

    1. Select UIView in which we want to update constraints. Here we need to update width, So we will add constraint for width.

    1. Now we can see new constraint added to UIView.

    1. Click on that constraint. It will show constraint properties. Here now we want to decrease width from 1024 to 900, So that in constraint property change Relation property to Less Than or Equal. Similarly if you need to increase width from default then Relation will be Greater Than or Equal.

    1. Now create an IBOutlet variable for NSLayoutConstraint and connect it with above width constraint.

    The variable looks like:

    Objective C Code:

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

    Swift Code:

    @IBOutlet weak var viewWidthConstraint : NSLayoutConstraint!
    

    1. Code to change constant as we need:

    To reduce constant we use below code:

    Objective C Code:

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

    Swift Code:

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

    And similarly we can change it to default:

    Objective C Code:

    // Change to default width of view.
    [UIView animateWithDuration:0.35f animations:^{
        self.viewWidthConstraint.constant = 1024;
        [self.view layoutIfNeeded];
    }];
    

    Swift Code:

    // Change to default width of view.
    UIView.animateWithDuration(0.35, animations: { () -> Void in
        self.viewWidthConstraint.constant = 1024
        self.view.layoutIfNeeded()
    })
    

    这篇关于移动与约束意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆