iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 21
0
Mobile Development

從零開始-30日練習開發iOS APP系列 第 21

從零開始-30日練習開發iOS APP-StackView Day-21

  • 分享至 

  • twitterImage
  •  

前言:
多個 UIButton 合併在一起的用法

正文:
先建立幾個 UIButton
https://ithelp.ithome.com.tw/upload/images/20201004/20129715WkI2LsEU22.png
全部選取,然後點擊 Stack View
https://ithelp.ithome.com.tw/upload/images/20201004/20129715v5HPkfYlW3.png
將 1234 這4個 UIButton 連接到 Outlet Collection
https://ithelp.ithome.com.tw/upload/images/20201004/20129715lFqlS7LGQo.png
利用Control鍵 + 滑鼠左鍵 將 firstButton 拖曳到 Safe Area 點選 Equal Hieghts
https://ithelp.ithome.com.tw/upload/images/20201004/20129715g7T5yxUE0t.png
最後加上約束,隱藏1234 這幾個 UIButton
https://ithelp.ithome.com.tw/upload/images/20201004/20129715Mn66ikvzyV.png

以下為程式碼:
ViewController

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var firstButton: UIButton!
    //將1234 UIButton 連結到options
    @IBOutlet var options: [UIButton]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    //將 firstButton UIButton 連結到 optionpressed
    @IBAction func selectOptions(_ sender: UIButton) {
        for option in options{
            UIView.animate(withDuration: 0.3, animations: {
                option.isHidden = !option.isHidden
            })
        }
    }
    //將1234 UIButton 連結到 optionpressed
    @IBAction func optionpressed(_ sender: UIButton) {
        let number = sender.currentTitle ?? ""
        firstButton.setTitle("\(number)", for: .normal)
        for option in options{
            UIView.animate(withDuration: 0.3, animations: {
                option.isHidden = !option.isHidden
            })
        }
    }

}

成果圖:


上一篇
從零開始-30日練習開發iOS APP-本地推播 Day-20
下一篇
從零開始-30日練習開發iOS APP-YouTubeVideo Player Day-22
系列文
從零開始-30日練習開發iOS APP30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言