gpt4 book ai didi

ios - 滑入一个 UIButton 并在达到一定距离时按下另一个 UIButton

转载 作者:行者123 更新时间:2023-11-29 12:19:41 27 4
gpt4 key购买 nike

我想从左向右滑入一个 UIButton(屏幕截图中的灰色)并将其置于屏幕中央。虽然已经有一个 UIButton(屏幕截图中的橙色)居中,但应将此按钮推到右侧,并且始终与滑入的 UIButton 保持 20 的距离。

enter image description here

居中按钮(橙色)有四个约束:

  • 高度限制
  • 宽度限制
  • 布局指南约束的顶部空间
  • 水平约束

目前将滑入的按钮(灰色)有以下限制:

  • 中心 Y 与橙色按钮的关系
  • 高度限制
  • 宽度限制
  • 引导空间到superview

我的想法是为灰色 UIButton 的“前导空间”约束添加一个导出,并使用 UIView 的 animateWithDuration:animations: 方法为其设置动画:

- (void)viewDidLoad {
[super viewDidLoad];

self.slideInLeadingConstraint.constant = 100;
[self.slideInButton setNeedsUpdateConstraints];
[UIView animateWithDuration:3.0 animations:^{
[self.slideInButton layoutIfNeeded];
[self.centerButton layoutIfNeeded];
}];
}

为了使它起作用,我可以在约束中更改什么?

最佳答案

  1. 由于您希望橙色按钮在某个点移动,因此您不能强制它居中。将优先级从 1000 降低到更低的值。这意味着:我真的希望它居中,但不一定非得居中。

  2. 在两个按钮之间添加水平距离(前导/尾随)约束。将其值设置为 >= 20。这意味着:距离不能低于 20pt,但任何更大的都可以。

  3. 动画化 ;)
    在您的动画 block 中,您需要对动画中涉及的所有 View 或其公共(public) super View 之一调用 layoutIfNeeded

Note:
The reason why you had problems with this is that you have called it from viewDidLoad. At that point, the view has been fully initialized, but not added to the hierarchy yet.
It's not yet certain, how big the parent view will be and dimensions for the animation will have to be guessed (by just taking the dimensions of your scene in the storyboard).
Performing the animation after it has been initially displayed (e.g. in viewDidAppear) causes the animation to be calculated with the right dimensions.

关于ios - 滑入一个 UIButton 并在达到一定距离时按下另一个 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com