1. 程式人生 > >Swift:我的第一個Demo(UILabel和UIButton)

Swift:我的第一個Demo(UILabel和UIButton)

完成的邏輯,按下按鈕從hello變成感謝點選,鬆手還原

檔名:AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        let rootview = ViewController()
        window?.rootViewController = rootview
        window?.makeKeyAndVisible()
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

 檔名:ViewController.swift

import UIKit

class ViewController: UIViewController {

    private var label: UILabel! // 用私有變數來保證變數的安全性
    private var btn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        view1.backgroundColor = UIColor.yellow
        self.view.addSubview(view1)
        
        label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        label.text = "hello"
        label.layer.borderWidth = 2
        label.tag = 1
        label.textAlignment = NSTextAlignment.center
        label.center = CGPoint(x: view1.bounds.width / 2, y: 400)
        view1.addSubview(label)
        
        btn = UIButton(frame: CGRect(x: 0, y: 0, width:250, height: 150))
        btn.setTitle("Click Me", for: .normal)
        btn.setTitle("Now you are pressing me", for: .highlighted)
        btn.center = CGPoint(x: view1.bounds.width / 2, y: 700)
        btn.backgroundColor = UIColor.red
        btn.layer.cornerRadius = 2
        btn.layer.borderWidth = 2
        btn.addTarget(self, action: #selector(self.btnTouchDown(sender:)), for: UIControl.Event.touchDown) // 這裡是為按鈕新增事件觸發對應的操作,self那一欄裝的是物件,這裡的物件指的就是該類本身,所以用self來指代,後面的action指的是觸發條用的方法,語法格式用#selector(self.函式名()實現,注意,如果含有引數,則這裡的sender後面必須要加冒號)
        btn.addTarget(self, action: #selector(self.btnTouchUp(sender:)), for: UIControl.Event.touchUpInside)
        view1.addSubview(btn)
    }
    
    @IBAction
    func btnTouchDown(sender: UIButton) { // 按住按鈕label變為"感謝點選"
        let label = self.label.viewWithTag(1) as! UILabel //類都是引用型別,所以'='賦值的是引用,因此可以改變,z這裡用viewWithTag方法來找到Tag為1的label
        label.text = "感謝點選"
    }
    @IBAction // 鬆開手時,改回"hello"
    func btnTouchUp(sender: UIButton) {
        let label = self.label.viewWithTag(1) as! UILabel
        label.text = "hello"
    }
}