mercredi 2 janvier 2019

addGestureRecognizer not firing if statement in swift4

I am trying to use a if statement when the uses touches the imageview it changes the constraints. Right now my code changes from small to big but it does not change from big to small. So the if statement only works one time but does not work again.

 var counter = 0

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleRegisterC))
        FIRE.addGestureRecognizer(tap)
        FIRE.isUserInteractionEnabled = true
        print(counter)

    }
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

        view.addSubview(FIRE)
    FIRE.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    FIRE.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true



    FIRE.widthAnchor.constraint(equalToConstant: 400).isActive = true
    FIRE.heightAnchor.constraint(equalToConstant: 700).isActive = true
     FIRE.translatesAutoresizingMaskIntoConstraints = false



    @objc func handleRegisterC(sender: UIButton){

        counter += 1


        if counter % 2 == 0 {


            FIRE.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            FIRE.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true


            FIRE.widthAnchor.constraint(equalToConstant: 400).isActive = true
            FIRE.heightAnchor.constraint(equalToConstant: 700).isActive = true
            FIRE.translatesAutoresizingMaskIntoConstraints = false

        }
        if counter % 2 == 1 {
            FIRE.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
            FIRE.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
            FIRE.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
            FIRE.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
            FIRE.translatesAutoresizingMaskIntoConstraints = false


        }


    }



}

Aucun commentaire:

Enregistrer un commentaire