Move a view inside a view controller

I have a View controller and stack views set up as follows. Inside a Stack View, I have buttons. enter image description here

I have a Player class inside the view controller and I have a function in it to move a card. I need my busViewButton to move on top of BtnP1A and I tried to achieve it as follows:

class GameViewController: UIViewController, Alertable {
    @IBOutlet weak var btnP1A: UIButton!
    @IBOutlet weak var btnP1B: UIButton!
    var p1 = Player(n:"P1")
    
    func start() -> Void{
        p1.buttonA = btnP1A
        p1.buttonB = btnP1B
        p1.vc = self
    }
.....

    
    public class Player: Alertable{
        var buttonA:UIButton!
        var buttonB:UIButton!
        weak var vc: GameViewController! = nil

.....
        func move() -> Void{
            var busFrame = self.vc.busViewButton.frame
            
            //self.buttonA is BtnP1A
            let frameA = self.buttonA.superview?.convert(self.buttonA.frame.origin, to: nil)
            
            busFrame.origin.y = frameA!.y
            busFrame.origin.x = frameA!.x
            
            self.vc.busViewButton.frame = busFrame
        }
.....
    }
}

But this does not update the position correctly, the new position is different to the position of BtnP1A. Am I not getting the relative position correctly?

UPDATE:

It has something to do with being called from the nested class. Because, when a similar animation is called from the View Controller class, it works, but when moved inside the Player class, it does not work.

Upvotes: 0

Views: 332

Answers (1)

ONDER GULER
ONDER GULER

Reputation: 21

try this p1.buttonA.isHidden = true

If you make the visibility of the contents false in stackviews, the other views will go to the top or elsewhere (of course depends on your constraints).

Upvotes: 1

Related Questions