簡體   English   中英

我想在點擊它時更改UIButton圖像

[英]I want to change UIButton Image when tapped on it

我在CollectionViewController的標題中有2個按鈕。 當我點擊其中一個時,我正在使用UIControlState - > .normal .selected更改此按鈕的圖像。

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.delegate = self
    collectionView.dataSource = self

    engSwitchButton.setImage(#imageLiteral(resourceName: "abc"), for: .normal)
    geoSwitchButton.setImage(#imageLiteral(resourceName: "abg"), for: .normal)
    engSwitchButton.setImage(#imageLiteral(resourceName: "abc2"),for: UIControlState.selected)
    geoSwitchButton.setImage(#imageLiteral(resourceName: "abg2"), for: UIControlState.selected)


    engSwitchButton.tag = Language.english.rawInt
    geoSwitchButton.tag = Language.georgian.rawInt
}



@IBAction func languageSwitchTapped(_ sender: UIButton) {

    sender.isSelected = !sender.isSelected



    selectedLanguage = Language(rawInt: sender.tag)!
    collectionView.reloadData()
}

我想要首先點擊的按鈕,當我通過點擊它改變2d按鈕的狀態時,回到.normal狀態。

要在點擊時更改按鈕圖像,您應該使用UIControlState.highlighted

所以改變:

engSwitchButton.setImage(#imageLiteral(resourceName: "abc2"),for: UIControlState.selected)
    geoSwitchButton.setImage(#imageLiteral(resourceName: "abg2"), for: UIControlState.selected)

對此:

engSwitchButton.setImage(#imageLiteral(resourceName: "abc2"),for: UIControlState.highlighted)
    geoSwitchButton.setImage(#imageLiteral(resourceName: "abg2"), for: UIControlState.highlighted)
@IBAction func yourFirstButton(_ sender: Any) {
    firstButton.setImage(UIImage(named: "yourButtonPressedImage")!, for: .normal)
    secondbutton.setImage(UIImage(named: "yourNormalImage")!, for: .normal)


}

在你的第二個按鈕@IBAction方法只需切換按鈕的圖像

 @IBAction func yourSecondButton(_ sender: Any) {
     secondButton.setImage(UIImage(named: "yourButtonPressedImage")!, for: .normal)
     firsBbutton.setImage(UIImage(named: "yourNormalImage")!, for: .normal)


 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM