import UIKit
@UIApplicationMain //main函数
// 应用程序代理类
//AppDelegate中的方法都是UIApplicationDelegate中的协议方法
//UIApplication 应用程序类
class AppDelegate: UIResponder, UIApplicationDelegate {
// 应用程序窗口,是AppDelegate类色属性
var window: UIWindow?
//应用程序加载完成时触发这个方法
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// 想在window对象添加内容,就在这个方法中实现
//UIScreen 屏幕类
//UIScreen.main 获取屏幕对象
//UIScreen.main.bounds 屏幕大小
self.window = UIWindow(frame: UIScreen.main.bounds)
//颜色
self.window?.backgroundColor = #colorLiteral(red: 1, green: 0.6225224255, blue: 0.6268012779, alpha: 1)
//让window成为应用程序的主窗口,并使其可见
self.window?.makeKeyAndVisible()
//给window设置根视图控制器(现在只做了解)
self.window?.rootViewController = UIViewController()
//(一般一个应用程序只需要一个UIWindow对象)
/*
//UIView的创建方式(五个视图)
let redView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
redView.backgroundColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
//向window上添加一个子视图
self.window?.addSubview(redView)
//获取屏幕的宽
let screenWidth = UIScreen.main.bounds.size.width
//获取屏幕的高
let screenHeight = UIScreen.main.bounds.size.height
let greenView = UIView(frame: CGRect(x: screenWidth - 100, y: 0, width:100 , height: 100))
greenView.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
self.window?.addSubview(greenView)
let yellowView = UIView(frame: CGRect(x: 0, y: screenHeight - 100, width: 100, height: 100))
yellowView.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)
self.window?.addSubview(yellowView)
let perploView = UIView(frame: CGRect(x: screenWidth - 100, y: screenHeight - 100, width: 100, height: 100))
perploView.backgroundColor = #colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)
self.window?.addSubview(perploView)
let yellow = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
//yellow中心点和window中心点重合
yellow.center = (self.window?.center)!
yellow.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
self.window?.addSubview(yellow)
*/
let centerView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
centerView.backgroundColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, 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.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
//向centerView上添加子视图
let greenView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
greenView.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
//子视图超出父视图的边界,就把超出部分剪掉了(了解)
//centerView.clipsToBounds = true
//tag值属性 给一个视图添加一个唯一标识(0~100不要在使用了)
greenView.tag = 200
centerView.addSubview(greenView)
//subViews属性,获取子视图的属性,返回值是一个数组,
let arr = centerView.subviews
let newView = arr[0]
newView.backgroundColor = UIColor.blue
//根据tag值获取视图对象
let newView2 = centerView.viewWithTag(200)
newView2?.backgroundColor = UIColor.cyan
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:.
}
}