2016-08-28 3 views
1

このコードには、ボタンの上にある青い四角形を押すと、ボタンの上にある小さな青い四角形が小さくなるボタンがあります。しかし、もう一度ボタンを押すと元のサイズに戻りません。これは、ボタン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. 
    } 


} 
+0

答えがあなたの問題を解決する場合は、答えの左側のチェックマークをクリックして問題を解決してください。あなたがこれまでに尋ねたすべての質問のためにそれを行ってください。 – vacawama

答えて

0

これを行う方法が1つあります。すべての制約をallcという配列に入れます。制約の半分だけがいつでもアクティブになります。その後、あなたのボタンのハンドラで

、非アクティブにアクティブな制約を変更し、以前にアクティブでないものがアクティブになります

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) 
    } 
} 

注:最初に新しいものをアクティブにする前に制約を無効にする必要があります。最初に非アクティブにすると、ビューが拘束された状態が回避され、自動レイアウトによって警告メッセージが表示されます。このため、コードでは、新しい制約を配列にアクティブにして、の後にをアクティブにできるようにします。これは、以前アクティブな制約が無効になっています。

関連する問題