Swift-UIKit 01-03

  • UIKitSwift01
效果图-01.png

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
效果图2.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.
        //想在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
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,066评论 4 62
  • 入手魅蓝metal第一天,耍了个遍,确实很棒。
    小小醉书虫阅读 136评论 0 0
  • 在车上一言不发,脖子上套上一个圈子,我要伪装成一个慵懒的胖子。往后一靠,按照很得劲,身边的老阿姨一直在看一本《西藏...
    dec41dfda297阅读 183评论 0 0
  • 从O4年至今17年一直都在工厂上班每天早上六点去上班晚上都要到十一点下班,这样口日子真不过,不也习惯
    安弟阅读 167评论 0 0
  • 窗外阴雨绵绵, 内心思绪万千。 考场各有心思, 一切皆有可能。
    588珊阅读 378评论 0 0