gpt4 book ai didi

xcode - 试图将 UIView 移出屏幕并使用自动布局

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:50 25 4
gpt4 key购买 nike

我有一个 UIView,我想在屏幕外设置动画,然后将另一个 View 带入屏幕。我正在使用自动布局,并且为左右 NSLayoutConstraint 设置了 IBOutlet。看起来如果没有为 UIView 设置宽度, View 实际上不会在屏幕上显示动画。

我正在尝试使用此代码关闭 UIView:

  self.gameViewConstraintLeft.constant = -400
UIView.animateWithDuration(1, animations: { () in
self.view.layoutIfNeeded()
})

self.gameViewConstriantRight.constant = -400
UIView.animateWithDuration(1, animations: { () in
self.view.layoutIfNeeded()
})

我在顶部 (0)、左侧 (0) 和右侧 (0) 设置了约束。也许这是我将 View 固定到屏幕边缘的问题,但我希望它适用于多种屏幕尺寸,所以我不确定实现我的目标的最佳方法。

最佳答案

如果 View 水平或垂直固定在多边上,您将无法移动 View 。

我建议如下:

  1. 为您的两个 View 设置宽度、高度、centerX 和 centerY 约束。可以将 super View 的宽度和高度设置为“等宽”和“等高”,以允许它在多种屏幕尺寸下工作。

  2. IBOutlet 添加到 centerX 约束。通过修改此约束的 constant 属性来设置动画:

    self.gameView1ConstraintCenterX.constant = -400
    UIView.animateWithDuration(1) {
    self.view.layoutIfNeeded()
    }

    self.gameView2ConstriantCenterX.constant = 0
    UIView.animateWithDuration(1) {
    self.view.layoutIfNeeded()
    }

关于xcode - 试图将 UIView 移出屏幕并使用自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002564/

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