当用户接触到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
}
}
}
答案 0 :(得分:0)
我不认为if statement
只工作一次(您可以在每个块中添加打印语句以进行确认)。我认为问题在于您有两组同时处于活动状态的约束。您可能想要做的是在这两组之间交替进行;将其中一个设为活动状态,将另一个设为非活动状态。试试这样的东西
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)
}