簡體   English   中英

swift4中的if語句不會觸發addGestureRecognizer

[英]addGestureRecognizer not firing if statement in swift4

我試圖在用戶觸及imageview時使用if語句,它會更改約束。 現在,我的代碼從小到大變化,但從大到小都沒有變化。 因此,if語句只能工作一次,而不能再次工作。

 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


        }


    }



}

我認為if statement僅工作一次(您可以在每個塊中放置一個print語句進行確認)。 我認為問題在於您有兩組同時處於活動狀態的約束。 您可能想要做的是在這兩組之間交替進行; 將其中一個設為活動狀態,將另一個設為非活動狀態。 試試這個

enum Size {
    case big
    case small
}
var counter = 0

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(FIRE)
    changeSizeTo(.small)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleRegisterC))
    FIRE.addGestureRecognizer(tap)
    FIRE.isUserInteractionEnabled = true
}

private func changeSizeTo(_ size: Size) {
    FIRE.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = size == .small
    FIRE.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = size == .small
    FIRE.widthAnchor.constraint(equalToConstant: 400).isActive = size == .small
    FIRE.heightAnchor.constraint(equalToConstant: 700).isActive = size == .small
    FIRE.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = size == .big
    FIRE.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = size == .big
    FIRE.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = size == .big
    FIRE.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = size == .big
    FIRE.translatesAutoresizingMaskIntoConstraints = false
}

@objc func handleRegisterC(sender: UIButton){
    counter += 1
    changeSizeTo(counter % 2 == 0 ? .small : .big)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM