如何改变前后约束的按钮(swift代码)? [英] how to change to before and after constraints when pressing button (swift code)?

查看:197
本文介绍了如何改变前后约束的按钮(swift代码)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码有一个按钮,当按下按钮上方的蓝色方块变小。但是当再次按下按钮时,正方形不会恢复其原始大小。这是因为按钮func只有一个命令。我如何通过只按一个按钮来命令按钮在原始尺寸和较小尺寸之间来回移动。这是swift代码。

This code has a button that when pressed the blue square above the button becomes smaller. But when pressing the button again the square does not revert back to its original size. This is because the buttons func only has one command. How can I command the button to go back and forth between its original size and smaller size by only pressing one button. This is swift code.

//
//  ViewController.swift
//  ssfsfd
//
//  Created by John Zalubski on 8/28/16.
//  Copyright © 2016 John Zalubski. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let colorview = UIView()
    var initialc = [NSLayoutConstraint]()
    override func viewDidLoad() {
        super.viewDidLoad()
        colorview.translatesAutoresizingMaskIntoConstraints = false
        colorview.backgroundColor = UIColor.blueColor()
        self.view.addSubview((colorview))

        let leadingc = colorview.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor)
        let trailingC = colorview.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor)
        let topc = colorview.topAnchor.constraintEqualToAnchor(self.view.topAnchor)
        let bottomc = colorview.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor, constant: -50)

        initialc.appendContentsOf([leadingc,trailingC,topc,bottomc])
        NSLayoutConstraint.activateConstraints(initialc)


    }
    @IBAction func changethebleep(sender: AnyObject) {

        NSLayoutConstraint.deactivateConstraints(initialc)

        let widthc = colorview.widthAnchor.constraintEqualToConstant(100)
        let heightc = colorview.heightAnchor.constraintEqualToConstant(100)
        let centerxc = colorview.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor)
        let centeryc = colorview.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor)

        NSLayoutConstraint.activateConstraints([widthc,heightc,centerxc,centeryc])

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


推荐答案

这里有一种方法。将所有约束放在名为 allc 的数组中。

Here is one way to do it. Put all of the constraints in an array called allc. Only half of the constraints will be active at any time.

在您的按钮处理程序中,将活动约束更改为非活动约束,然后使以前处于非活动状态的约束处于活动状态: / p>

In your button handler, change the active constraints to inactive, and then make the previously inactive ones be active:

class ViewController: UIViewController {

    let colorview = UIView()
    var allc = [NSLayoutConstraint]()
    override func viewDidLoad() {
        super.viewDidLoad()
        colorview.translatesAutoresizingMaskIntoConstraints = false
        colorview.backgroundColor = UIColor.blueColor()
        self.view.addSubview((colorview))

        let leadingc = colorview.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor)
        let trailingC = colorview.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor)
        let topc = colorview.topAnchor.constraintEqualToAnchor(self.view.topAnchor)
        let bottomc = colorview.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor, constant: -50)

        NSLayoutConstraint.activateConstraints([leadingc, trailingC, topc, bottomc])

        let widthc = colorview.widthAnchor.constraintEqualToConstant(100)
        let heightc = colorview.heightAnchor.constraintEqualToConstant(100)
        let centerxc = colorview.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor)
        let centeryc = colorview.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor)

        allc = [leadingc, trailingC, topc, bottomc, widthc, heightc, centerxc, centeryc]
    }

    @IBAction func changethebleep(sender: AnyObject) {

        var newactive = [NSLayoutConstraint]()

        for constraint in allc {
            if constraint.active {
                constraint.active = false
            } else {
                newactive.append(constraint)
            }
        }

        NSLayoutConstraint.activateConstraints(newactive)
    }
}

注意:必须先激活约束,然后再激活新约束。如果您首先停用,您将避免您的视图超限的情况,并且Auto Layout会发出警告消息。这就是为什么代码将新的约束激活到数组中,以便在之前激活它们。

Note: It is necessary to first deactivate constraints before activating new ones. If you deactivate first, you will avoid the situation where your view is over constrained and Auto Layout gives warning messages. This is why the code puts the new constraints to activate into an array so that it can activate them after the previously active ones have been deactivated.

这篇关于如何改变前后约束的按钮(swift代码)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆