2017-01-02 1 views
0

私のコードでは、UIButtonを画面の中央で開始したいのですが、クリックした後、画面上の位置。だから私はそれをクリックした後にランダムな位置に移動するようにコードを書いていますが、問題は中心から始まるのではなくランダムな位置から始まっているということです。ここに私のViewController.swiftのコードは次のとおりです。あなたがストーリーボードにUIbuttonを持っているよう私のUIButtonは、中央で開始するのではなくランダムな位置から開始しています

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 

} 

}

+1

そこにはレイアウト情報は、位置をフレームに関連するすべてのものは、したがって、存在しない場所、中心、...ランダム/定義/設定されていません。例えば ​​'viewDidLayoutSubviews'を試してください。 – luk2302

答えて

1

@ luk2302がコメントで指摘したように、viewDidLoadで正しいビューフレームに頼ることはできません。そのロジックをviewDidLayoutSubviewsに移動する必要があります。

ただし、システムによってviewDidLayoutSubviewsが呼び出された場合、ボタンは元の位置に戻ります。

ムービーを移動したらランダム化された位置に保持したい場合は、「wasRandomized」ブール型インスタンス変数を追加して、viewDidLayoutSubviewsのロジックをランダム化していない場合はボタンを中央に移動させます既に。

実際にレイアウト制約を使用する必要があることにも注意してください。 x位置のレイアウト制約とyのレイアウト制約を作成し、ボタンを移動するときにその定数値を変更します。そうすれば、自動レイアウトはボタンの位置をあなたの下から移動させません。

0

は思えます。 UIButtonがcneterで開始するようにするには、コード内でもコンテナ

  • 一定の幅と高さ
  • に垂直方向にコンテナ

  • に水平に

    1. としてストーリーボードにUIButtonのレイアウト制約を設定viewDidLoad

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

      は必須ではありません。

  • 0

    実際には制約を使用する必要があります。あなたがストーリーボード内で作業したくないためにそれらを使用していない場合は、それらをプログラム的に設定する方法があります。

    重要:アップルでは、​​この方法を使用したくないが、私はこの方法を制約の理解に役立てて、ストーリーボードへの移行をはるかに簡単にした。あなたが興味を持っているなら、ここにビデオガイドがあります。 viewDidLoad` `で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 
    
    +0

    私はすでに必要な答えを得ている – krish

    関連する問題