Reputation: 63
When a button at my app is pressed, it calls addSubView()
to add a new view in the superView of my ViewController. The problem is that a have 2 buttons to move 2 other views of 100x100 at this same superView, and when i call addSubView(newView), the positions of my other 2 views reset to the origin point.
My question is, how can i add a newView preserving the other views coordinates?
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.
}
}
Upvotes: 2
Views: 466
Reputation: 154523
Autolayout is running and moving your views. There are 2 ways you can deal with this:
1) Define your bloco1
and bloco2
views entirely in code. Don't define them in Interface Builder and don't use @IBOutlets
. If you do this, Autolayout will not touch your views and they will stay where you put them.
2) If you want to define your views with Interface Builder, then you should not move them by altering their frames. Instead, you should create @IBOutlet
s to the constraints which position the views, then move the views by altering the constant
property of the layout constraints.
To create the @IBOutlet
to a constraint, find the constraint in the Document Outline view, and control-drag from that constraint to the ViewController in the code. Give it a name like centerXConstraint
. It will appear in the code like this:
@IBOutlet weak var centerXConstraint: NSLayoutConstraint!
Then to move the view, just update the constant
property:
centerXConstraint.constant += 10
Upvotes: 1