2015-09-26 19 views
1

當我的應用程序按鈕被按下時,它會調用addSubView()在我的ViewController的超級視圖中添加一個新視圖。問題是,有2個按鈕來移動2個其他視圖100x100在這個相同的超視圖,當我調用addSubView(newView),我的其他2視圖的位置重置爲原點。爲什麼.addSubView(...)方法在使用時重置其他對象位置?

我的問題是,我如何添加一個保存其他視圖座標的新視圖?

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 
    @IBOutlet weak var bloco1: UIView! 
    @IBOutlet weak var bloco2: UIView! 
    @IBOutlet weak var botao1: UIButton! 
    @IBOutlet weak var botao2: UIButton! 

    var bloco1Origem: CGPoint! 
    var bloco2Origem: CGPoint! 
    var corBlocos: UIColor! 
    var reprodutorDeAudio: AVAudioPlayer? 
    var somDoAlerta: NSURL! 
    var novaView: UIView! 
    var qntBlocosNovos = 0 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let path = NSBundle.mainBundle().pathForResource("SomDeColisão", ofType: "mp3") 
     somDoAlerta = NSURL(fileURLWithPath: path!)  
     do { 
      reprodutorDeAudio = try AVAudioPlayer(contentsOfURL: somDoAlerta) 
      reprodutorDeAudio!.prepareToPlay() 
     } catch { 
      print(error)   
     } 
    } 

    override func viewWillAppear(animated: Bool) { 
     bloco1.backgroundColor = corBlocos 
     bloco2.backgroundColor = corBlocos 
    } 

    override func viewDidAppear(animated: Bool) { 
     bloco1Origem = bloco1.frame.origin 
     bloco2Origem = bloco2.frame.origin 
    } 

    @IBAction func AddNovoBloco(sender: UIBarButtonItem) { 
     novaView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) 
     novaView.backgroundColor = UIColor.yellowColor() 
     self.view.addSubview(novaView) 
     //self.view.didAddSubview(novaView) 
     ++qntBlocosNovos 
    } 

    @IBAction func botao(sender: UIButton) { 
     bloco1.frame.origin.x += 10 
     if colisao() { 
      bloco2.backgroundColor = UIColor.redColor() 
      botao1.enabled = false 
      botao2.enabled = false 
     } 
//  view2.transform = CGAffineTransformMakeRotation(10) 
    } 

    @IBAction func excluirTodos(sender: UIButton) { 
     while qntBlocosNovos > 0 {   
      (view.subviews.last! as UIView).removeFromSuperview() 
      --qntBlocosNovos 
     } 
    } 

    @IBAction func botao2(sender: UIButton) { 
     if qntBlocosNovos > 0 {  
      (view.subviews.last! as UIView).removeFromSuperview() 
     } 
     bloco2.frame.origin.x -= 10 
     if colisao() {   
      bloco1.backgroundColor = UIColor.redColor() 
      botao1.enabled = false 
      botao2.enabled = false 
     } 
    } 

    func colisao() -> Bool { 
     if CGRectIntersectsRect(bloco1.frame, bloco2.frame) { 
      guard let rep = reprodutorDeAudio else { 
       return true    
      } 
      rep.play() 
      return true 
     } 
     return false 
    } 

    @IBAction func Reinicia(sender: UIButton) { 
     bloco1.frame.origin = bloco1Origem 
     bloco2.frame.origin = bloco2Origem 
     botao1.enabled = true 
     botao2.enabled = true 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 
+0

請發表您的代碼 – Lamar

+0

只是增加了它:) – Renan

回答

1

自動佈局運行和移動你的意見。有兩種方法可以解決這個問題:

1)完全用代碼定義您的bloco1bloco2視圖。不要在Interface Builder中定義它們,也不要使用@IBOutlets。如果你這樣做,Autolayout不會觸及你的意見,他們會留在你把它們放在你的位置。

2)如果你想用界面生成器定義你的視圖,那麼你不應該通過改變它們的幀來移動它們。相反,您應該爲定位視圖的約束創建@IBOutlet,然後通過更改佈局約束的constant屬性來移動視圖。

要創建@IBOutlet的約束,發現在文檔大綱視圖的約束,並從代碼限制到的ViewController控制 -drag。給它起一個名字,如centerXConstraint。它會出現在這樣的代碼:

@IBOutlet weak var centerXConstraint: NSLayoutConstraint! 

然後移動視圖,只需更新constant屬性:

centerXConstraint.constant += 10 
+0

謝謝你很多! !!!!真的:) – Renan

相關問題