移动与约束意见 [英] Moving views with constraints
问题描述
我在我的视图控制器一对夫妇的意见当检测到了刷卡再往下检测向下滑动时向上移动。我被强迫的意见通过调整使用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
。
以下几点我也要跟着来实现:
- 选择
的UIView
中,我们要更新的限制。在这里,我们需要更新宽度
,所以我们将增加约束的宽度。
<醇开始=2>
的UIView
<醇开始=3>
<醇开始=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:
- Select
UIView
in which we want to update constraints. Here we need to updatewidth
, So we will add constraint for width.
- Now we can see new constraint added to
UIView
.
- 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.
- Now create an
IBOutlet
variable forNSLayoutConstraint
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!
- 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屋!