import UIKit
@UIApplicationMain
//應用程序代理類
//AppDelegate中的方法都是UIApplicationDelegate中的協議方法
//UIApplication 應用程序類
class AppDelegate: UIResponder, UIApplicationDelegate {
//應用程序窗口,是AppDelegate這個類的一個屬性
var window: UIWindow?
//應用程序加載完成的時候觸發這個方法
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//想在window對象添加內容,就在這個方法中實現
//UIScreen屏幕類
//UIScreen.main獲取屏幕對象
//UIScreen.main.bounds
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
//讓window成為應用程序的主窗口,并使其可見
self.window?.makeKeyAndVisible()
//給window設置跟試圖控制器(現在只做了解)
//一般應用程序只有一個window對象
self.window?.rootViewController = UIViewController()
//UIView創建方式
/*let redView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
redView.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
//向window添加一個子視圖
self.window?.addSubview(redView)
//獲取屏幕的寬
let screenWidth = UIScreen.main.bounds.size.width
//高
let screenHight = UIScreen.main.bounds.size.height
let greenView = UIView(frame: CGRect(x: screenWidth - 100, y: 0, width: 100, height: 100))
greenView.backgroundColor = #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)
self.window?.addSubview(greenView)
let purpleView = UIView(frame: CGRect(x: 0, y: screenHight - 100, width: 100, height: 100))
purpleView.backgroundColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
self.window?.addSubview(purpleView)
let magentaView = UIView(frame: CGRect(x: screenWidth - 100, y: screenHight - 100, width: 100, height: 100))
magentaView.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
self.window?.addSubview(magentaView)
let yellowView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
//yellowview中心點和window中心店重合
yellowView.center = (self.window?.center)!
yellowView.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
self.window?.addSubview(yellowView)*/
let centerView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
centerView.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
self.window?.addSubview(centerView)
//UIView的常用屬性
//alpha 透明度 0.0~1.0
centerView.alpha = 1.0
//hidden 顯隱性 true隱藏 false顯示(默認值)
centerView.isHidden = false
//superView 獲取到父視圖的屬性
let fatherView = centerView.superview
fatherView?.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
//向center添加子視圖
let greenView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
greenView.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
//子視圖超出父視圖的邊界,就把超出部分給剪掉了
//centerView.clipsToBounds = true
//tag值屬性 給一個視圖添加一個唯一的標示(0~100不要在使用了)
centerView.tag = 200
centerView.addSubview(greenView)
//subView屬性,獲取子視圖屬性 返回值是一個數組
let arr = centerView.subviews
let newView = arr[0]
newView.backgroundColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
//print(arr.count)
//根據tag值獲取視圖對象
let newView2 = centerView.viewWithTag(200)
newView2?.backgroundColor = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)
let newView3 = self.window?.viewWithTag(200)
newView3?.backgroundColor = UIColor.red
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:.
}
}