【问题标题】:My UIButton is starting in a random position instead of starting in the center and moving我的 UIButton 从随机位置开始,而不是从中心开始并移动
【发布时间】:2017-01-02 15:50:31
【问题描述】:

在我的代码中,我希望 UIButton 从屏幕中心开始,然后单击它后,我希望它移动到屏幕上的随机位置。所以我把它的代码放在我点击它之后去一个随机位置,虽然问题是它是从一个随机位置开始而不是从中心开始。这是我的 ViewController.swift 的代码:

class ViewController: UIViewController {
@IBOutlet weak var btn: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    btn.layer.cornerRadius = 10
    btn.layer.position = CGPoint(x: self.view.center.x, y: self.view.center.y)
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func tapRandomBTN(_ sender: Any) {
    let buttonWidth = btn.frame.width
    let buttonHeight = btn.frame.height

    // Find the width and height of the enclosing view
    let viewWidth = btn.superview!.bounds.width
    let viewHeight = btn.superview!.bounds.height

    // Compute width and height of the area to contain the button's center
    let xwidth = viewWidth - buttonWidth
    let yheight = viewHeight - buttonHeight

    // Generate a random x and y offset
    let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
    let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))

    // Offset the button's center by the random offsets.
    btn.center.x = xoffset + buttonWidth / 2
    btn.center.y = yoffset + buttonHeight / 2

}

}

【问题讨论】:

  • viewDidLoad 中没有布局信息,因此与框架、位置、位置、中心...相关的所有内容都没有设置/定义/随机。试试viewDidLayoutSubviews 例如

标签: ios swift uibutton swift3


【解决方案1】:

正如@luk2302 在他的评论中指出的那样,您不能依赖 viewDidLoad 中正确的视图框架。您需要将该逻辑移至 viewDidLayoutSubviews。

但是,请注意,如果有任何事情导致系统调用 viewDidLayoutSubviews,您的按钮将快速回到原来的位置。

如果您希望它在移动后保持在其随机位置,您应该添加一个“wasRandomized”布尔实例变量,并使 viewDidLayoutSubviews 中的逻辑仅在您尚未随机化时将按钮移动到中心.

还请注意,您确实应该使用布局约束。为 x 位置创建一个布局约束,为 y 创建另一个布局约束,并在您想要移动按钮时修改它们的常量值。这样,自动布局不会将您的按钮位置从您下方移出。

【讨论】:

    【解决方案2】:

    好像你在 Storyboard 中有 UIbutton。要使 UIButton 从 cneter 开始,请将 Storyboard 中 UIButton 的布局约束设置为

    1. 水平放置在容器中
    2. 垂直放置在容器中
    3. 恒定的宽度和高度

    还有viewDidLoad中的代码

    btn.layer.position = CGPoint(x: self.view.center.x, y: self.view.center.y)
    

    不是必需的。

    【讨论】:

      【解决方案3】:

      你真的应该使用约束。如果您因为不想在情节提要中工作而没有使用它们,那么有一种方法可以以编程方式设置它们。不过它确实需要一些设置

      重要提示:Apple 不希望您使用这种方法,但我发现这种方法有助于理解约束,并且可以更轻松地过渡到使用情节提要。如果您有兴趣,这里有一个视频指南。 https://www.youtube.com/watch?v=lWSc0wHFTXM&t=6s

      //example of programatic constraints (swift3):
      
      button.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: randomX).isActive = true
      button.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: randomY).isActive = true
      button.widthAnchor.constraint(equalToConstant: 55.0).isActive = true
      button.heightAnchor.constraint(equalToConstant: 55.0).isActive = true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-14
        • 2017-11-19
        相关资源
        最近更新 更多