このコードには、ボタンの上にある青い四角形を押すと、ボタンの上にある小さな青い四角形が小さくなるボタンがあります。しかし、もう一度ボタンを押すと元のサイズに戻りません。これは、ボタンfuncは1つのコマンドしか持たないからです。 1つのボタンを押すだけで、元のサイズと小さいサイズの間で前後にボタンを移動させる方法を教えてください。これは迅速なコードです。ボタン(スウィフトコード)を押したときに前後の制約に変更する方法は?
//
// 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.
}
}
答えがあなたの問題を解決する場合は、答えの左側のチェックマークをクリックして問題を解決してください。あなたがこれまでに尋ねたすべての質問のためにそれを行ってください。 – vacawama