Search code examples
iosobjective-cswiftswift3swift5

how to make effects on button in ios.On clicking on that button it should expand


I want to apply effects on button.If click on that button,It should increase in size and other should remain in their original size.In the below image if clicked on jardine,it should expands.

enter image description here


Solution

  • Here is the code to animate and transform the button. It is run perfectly as you aspect

    func changeButtons(selectedButton:UIButton)
    {
        firstBtn.backgroundColor = .white
        firstBtn.setTitleColor(.black, for: .normal)
        animateButton(button: firstBtn, transformBy: .identity)
    
        secondBtn.backgroundColor = .white
        secondBtn.setTitleColor(.black, for: .normal)
        animateButton(button: secondBtn, transformBy: .identity)
    
        thirdBtn.backgroundColor = .white
        thirdBtn.setTitleColor(.black, for: .normal)
        animateButton(button: thirdBtn, transformBy: .identity)
    
        selectedButton.backgroundColor = .black
        selectedButton.setTitleColor(.white, for: .normal)
        animateButton(button: selectedButton, transformBy: CGAffineTransform(scaleX: 1.2, y: 1.2))
    }
    func animateButton(button : UIButton,transformBy:CGAffineTransform)
    {
        UIView.animate(withDuration: 0.2) {
            button.transform = transformBy
            self.view.layoutIfNeeded()
        }
    }
    @IBAction func btnFirstPress(_ sender: UIButton)
    {
        changeButtons(selectedButton: firstBtn)
    }
    @IBAction func btnSecondPress(_ sender: UIButton)
    {
        changeButtons(selectedButton: secondBtn)
    }
    @IBAction func btnThirdPress(_ sender: UIButton)
    {
        changeButtons(selectedButton: thirdBtn)
    }