我的视图控制器中有几个视图在检测到向上滑动时向上移动,然后在检测到向下滑动时向下移动.我通过使用 CGRectOffset 调整 y 原点来强制视图移动.我现在已经使用 IB 对我的视图应用了约束,但我不确定移动视图的最佳方式是什么,以便它们最终在 iphone 5、6 和 6+ 上处于正确的位置.
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];
}];
使用比率更改常数会更好吗?那么对于上面的约束,与其使用 338,不如这样做:
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 that you have to set your constraint appropriately.
我们来看一个例子.
我在 Storyboard 中有一个 UIView
,我想更改它的宽度.它的默认宽度是 1024
.
I have a UIView
in Storyboard and I would like to change its width. Its default width is 1024
.
经过一些动画,我们将其宽度改为900
.
After some animation, We will change it's width to 900
.
请按照以下步骤实现:
UIView
.这里我们需要更新width
,所以我们要为width添加一个约束.UIView
in which we want to update constraints. Here we need to update width
, So we will add a constraint for width.UIView
的新约束.UIView
.现在为 NSLayoutConstraint
创建一个 IBOutlet
变量,并将其与上述宽度约束连接起来.
Now create an IBOutlet
variable for NSLayoutConstraint
and connect it with above width constraint.
改变宽度常数
Swift 5.0
@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()
})
}
目标 C
@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];
}];
}
这篇关于移动带有约束的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!