为什么.addSubView(...)方法在使用时会重置其他对象的位置?

时间:2015-09-26 23:31:22

标签: ios swift

当按下我的应用程序按钮时,它会调用addSubView()在我的ViewController的superView中添加一个新视图。问题是有2个按钮可以在同一个superView中移动另外两个100x100的视图,当我调用addSubView(newView)时,我的其他2个视图的位置会重置为原点。

我的问题是,我如何添加一个保留其他视图坐标的newView?

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.
    }
}

1 个答案:

答案 0 :(得分:1)

Autolayout 正在运行并移动您的观看次数。有两种方法可以解决这个问题:

1)完全在代码中定义您的bloco1bloco2观看次数。不要在 Interface Builder 中定义它们,也不要使用@IBOutlets。如果您这样做, Autolayout 将不会触及您的观点,并且它们将保留在您放置它们的位置。

2)如果要使用 Interface Builder 定义视图,则不应通过更改其帧来移动它们。相反,您应该为定位视图的约束创建@IBOutlet,然后通过更改布局约束的constant属性来移动视图。

要为约束创建@IBOutlet,请在文档大纲视图中找到约束,并从该约束中找到 control -drag到代码中的ViewController 。给它命名为centerXConstraint。它将出现在代码中:

    @IBOutlet weak var centerXConstraint: NSLayoutConstraint!

然后移动视图,只需更新constant属性:

    centerXConstraint.constant += 10