美文网首页
Swift3.0UIView的创建

Swift3.0UIView的创建

作者: 赵果果 | 来源:发表于2016-11-21 09:45 被阅读0次

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

    }

    }

    相关文章

      网友评论

          本文标题:Swift3.0UIView的创建

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