colors - How to use a if else statement on constraints in swift -


the code below has uicolor box when pressed, box becomes smaller uicolor box. problem color box can pressed 1 time. box can go large small cant go small big. how can code written when box clicked box goes big small or small big every time.

import uikit  class viewcontroller: uiviewcontroller {      let colorview = uiview()     var initialc = [nslayoutconstraint]()     override func viewdidload() {         super.viewdidload()          let tapgesturerecognizer = uitapgesturerecognizer(target:self, action:#selector(viewcontroller.myfunction(_:)))         colorview.userinteractionenabled = true         colorview.addgesturerecognizer(tapgesturerecognizer)           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)       }      func myfunction(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 resources can recreated.     }   } 

you resizing view in awkward way. instead of using constraints, don't place them. tell view go frame. if need view animate change between frames use uiview.animateviewwithduration() , inside function frame changes desire. keeps transitions smooth , full control in hands. constraints not easy implement during run time frames.


Comments