- UIKitSwift01
AppDelegate.swift代码如下:
import UIKit
@UIApplicationMain
//应用程序类
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.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
//让window成为成为应用程序的主窗口,并使其可见
self.window?.makeKeyAndVisible()
//给window设置根视图控制器
self.window?.rootViewController = UIViewController()
//一般应用只有一个UIWindow对象
let blueView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
blueView.backgroundColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
//向window上添加子视图
self.window?.addSubview(blueView)
//获取屏幕的宽
let screenWidth = UIScreen.main.bounds.size.width
//获取屏幕的高
let screenHeight = UIScreen.main.bounds.size.height
let greenView = UIView(frame: CGRect(x: screenWidth - 150, y: 0, width: 150, height: 150))
greenView.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
self.window?.addSubview(greenView)
let yellowView = UIView(frame: CGRect(x: 0, y: screenHeight - 150, width: 150, height: 150))
yellowView.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
self.window?.addSubview(yellowView)
let grayView = UIView(frame: CGRect(x: screenWidth - 150, y: screenHeight - 150, width: 150, height: 150))
grayView.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
self.window?.addSubview(grayView)
let redView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
//redView中心点和window中心点重合
redView.center = (self.window?.center)!
redView.backgroundColor = #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)
self.window?.addSubview(redView)
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:.
}
}
- UIKitSwift02
AppDelegate.swift代码如下:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
//应用程序主窗口
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.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
//让window成为成为应用程序的主窗口,并使其可见
self.window?.makeKeyAndVisible()
//给window设置根视图控制器
self.window?.rootViewController = UIViewController()
let centerView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
centerView.backgroundColor = UIColor.red
self.window?.addSubview(centerView)
//UIView的常用属性
// alpha 透明度 0.0(透明) ~ 1.0(不透明)
centerView.alpha = 1.0
// isHidden 显隐性 true 隐藏 false 显示(默认值)
centerView.isHidden = false
// .superview 谁的父视图
let fatherView = centerView.superview
fatherView?.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)
//向centerView上添加子视图
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不要再使用)
greenView.tag = 200
centerView.addSubview(greenView)
//subView属性,获取子视图的属性,返回值是一个数组
let arr = centerView.subviews
let newView = arr[0]
centerView.backgroundColor = UIColor.blue
//根据tag值获取视图对象
let newView2 = centerView.viewWithTag(200)
newView2?.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
return true
}
- UIKitSwift03
![Uploading 03_906825.png . . .]
AppDelegate.swift代码如下:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)
self.window?.makeKeyAndVisible()
self.window?.rootViewController = UIViewController()
/*
//(UIView Layout) 视图布局
//frame bounds center
//frame决定的是一个视图,在他父视图的位置
let redView = UIView(frame: CGRect(x: 10, y: 20, width: 100, height: 150))
redView.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
self.window?.addSubview(redView)
redView.frame.origin = CGPoint(x: 100, y: 200)
redView.frame.size = CGSize(width: 200, height: 200)
// frame既能决定他在父视图的位置,也能控制他在父视图上的大小
print(UIScreen.main.bounds)
//bouds 视图自身的边界,bounds决定自身上子视图的位置bouds的origin点默认和视图本身坐标系的点是重合的
print(redView.bounds)
let greenView = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
greenView.backgroundColor = UIColor.green
redView.addSubview(greenView)
//不修改bounds的size, 修改bounds的origin
redView.bounds.origin = CGPoint(x: 50, y: 50)
print("中心点:\(redView.center)")
//无论一个视图的bounds怎么改变,这个视图的中心点都不会改变
*/
//视图层级关系
let a = UIView(frame: CGRect(x: 157, y: 200, width: 100, height: 100))
a.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
self.window?.addSubview(a)
let b = UIView(frame: CGRect(x: 107, y: 150, width: 200, height: 200))
b.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
self.window?.addSubview(b)
//最后添加在上层
//视图a放在最上层显示
//self.window?.bringSubview(toFront: a)
//视图b放在最下层
//self.window?.sendSubview(toBack: b)
//删除视图b
//b.removeFromSuperview()
print(self.window?.subviews)
// self.window?.exchangeSubview(at: <#T##Int#>, withSubviewAt: <#T##Int#>)
return true
}