美文网首页
ApplicationDelegate应用程序代理类

ApplicationDelegate应用程序代理类

作者: 浅笑嫣然9611 | 来源:发表于2016-11-28 19:51 被阅读0次

    class AppDelegate: UIResponder, UIApplicationDelegate {

    //应用程序窗口,是AppDelegate类的属性

    var window: UIWindow?

    //lazy var array:[Int] = Array()

    //应用程序加载完成时触发这个方法

    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: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)//输入color 回车  = UIColor.green

    //使window成为应用程序主窗口并使其可见

    self.window?.makeKeyAndVisible()

    //给window设置根视图控制器(现在只做了解)

    self.window?.rootViewController = UIViewController()

    //一般应用程序只有一个UIWindow对象

    //UIView的创建方式

    //1.创建一个子视图 左上角

    /*

    let radView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

    radView.backgroundColor = #colorLiteral(red: 1, green: 0.2527923882, blue: 1, alpha: 1)

    //向window上添加一个子视图

    self.window?.addSubview(radView)

    //获取屏幕的宽和高

    let screenWidth = UIScreen.main.bounds.size.width

    let screenHeight = UIScreen.main.bounds.size.height

    //2.创建一个子视图 右上角

    let greenView = UIView(frame: CGRect(x: screenWidth - 100, y: 0, width: 100, height: 100))

    greenView.backgroundColor = UIColor.green

    //向window上添加一个子视图

    self.window?.addSubview(greenView)

    //3.创建一个子视图 右下角

    let magentaView = UIView(frame: CGRect(x: screenWidth - 100, y: screenHeight - 100, width: 100, height: 100))

    magentaView.backgroundColor = UIColor.darkGray

    self.window?.addSubview(magentaView)

    //4.创建一个子视图 左下角

    let purpleView = UIView(frame: CGRect(x: 0, y: screenHeight - 100, width: 100, height: 100))

    purpleView.backgroundColor = UIColor.brown

    self.window?.addSubview(purpleView)

    //5.创建一个子视图 中心点

    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.9686274529, green: 0.78039217, blue: 0.3450980484, alpha: 1)

    self.window?.addSubview(yellowView)

    */

    let centerView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))

    centerView.backgroundColor = #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)

    self.window?.addSubview(centerView)

    //UIView的常用属性

    //alpha 透明度 0.0--1.0  1.0完全不透明

    centerView.alpha = 1.0

    //hidden显示隐藏  显隐性  true是隐藏 false显示(默认的)

    centerView.isHidden = false

    //superView 超视图获取到父视图的属性

    let fatherView = centerView.superview

    fatherView?.backgroundColor = UIColor.green

    //向centerview上添加子视图

    let greenView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

    greenView.backgroundColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)

    //子视图超出父视图的边界就把超出的部分剪掉(了解)

    //centerView.clipsToBounds = true

    //tag值属性 给一个视图添加一个唯一标识(0~100内不再使用)

    greenView.tag = 200//整数值

    centerView.addSubview(greenView)

    //获取subview属性,获取子视图属性 只能有一个父视图 多个子视图,返回值是一个数组,数组元素有序

    let arr = centerView.subviews

    let newView = arr[0]

    newView.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)

    //根据tag值获取视图对象

    let newView2 = centerView.viewWithTag(200)

    newView.backgroundColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)

    //        let newView3 = centerView.viewWithTag(200)

    //        newView.backgroundColor = #colorLiteral(red: 0.521568656, green: 0.1098039225, blue: 0.05098039284, alpha: 1)

    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:.

    }

    }

    相关文章

      网友评论

          本文标题:ApplicationDelegate应用程序代理类

          本文链接:https://www.haomeiwen.com/subject/kqnopttx.html