iOS-学习笔记-UI-第一天

来源:互联网 发布:学冬不拉软件 编辑:程序博客网 时间:2024/05/17 17:40

今天的目标:能够编写出第一个iOS APP程序,显示HelloWorld! 


1.如何创建一个iOS工程

        创建iOS项目,选择Empty模板。

2.如何运行一个iOS程序

    (1)点击左上角三角图标,运行在模拟器中,可以通过点击该图标旁边的选项,切换不同型号的模拟器

    (2)快捷键:Command+R 运行

    (3)快捷键:Command+B 编译

3.工程的文件组成

工程节点,工程配置文件,

(1)Frameworks:

            Foundation

            UIKit Framework

            Core Graphics

            

4.工程是如何启动的?项目执行顺序

    (1)main函数

  1. int main(int argc, char * argv[])

  2. {

  3.    @autoreleasepool {

  4.        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

  5.    }

  6. }

(启动后,创建了两个对象,一个监听时间的对象,一个处理具体事情的代理类对象。)


main函数做了三件事:

    第一件事:根据第三个参数创建了应用程序类对象,似然是nil,系统对于该参数的默认类型定位UIApplication类型,每个应用程序只有一个应用程序的对象实例,用于记录应用程序的当前状态,记录接收到用户的事件动作。

    第二件事:根据第四个参数创建了一个应用程序代理类的对象,用于根据应用程序对象的当前状态,提供相应。

    第三件事:启动了一个事件循环 ——死循环,不停的接受用户在屏幕上的操作事件,将该事件传给应用程序类对象。


  1. //

  2. //  AppDelegate.m

  3. //  1_HelloWorld

  4. //

  5. //  Created by 万齐鹣 on 15/6/2.

  6. //  Copyright (c) 2015年 万齐鹣. All rights reserved.

  7. //

  8. #import "AppDelegate.h"

  9. @implementation AppDelegate

  10. // 启动完成时候

  11. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

  12. {

  13.    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

  14.    // Override point for customization after application launch.

  15.    self.window.backgroundColor = [UIColor whiteColor];

  16.    

  17.    

  18.    int labelWidth = 100;

  19.    int labelHeight = 40;

  20.    int labelX = (self.window.frame.size.width-labelWidth)/2;

  21.    int labelY = 100;

  22.    

  23.    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelWidth, labelHeight)];

  24.    [label setText:@"HelloWorld!"];

  25.    label.textAlignment = NSTextAlignmentCenter;

  26.    

  27.    [self.window addSubview:label];

  28.    

  29.    [self.window makeKeyAndVisible];

  30.    return YES;

  31. }

  32. // 即将进入后台时候

  33. - (void)applicationWillResignActive:(UIApplication *)application

  34. {

  35.    // 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.

  36.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

  37. }

  38. // 已经进入后台

  39. - (void)applicationDidEnterBackground:(UIApplication *)application

  40. {

  41.    // 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.

  42.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

  43. }

  44. // 即将进入前台,

  45. - (void)applicationWillEnterForeground:(UIApplication *)application

  46. {

  47.    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

  48. }

  49. // 成为活动窗口

  50. - (void)applicationDidBecomeActive:(UIApplication *)application

  51. {

  52.    // 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.

  53. }

  54. // 结束的时候

  55. - (void)applicationWillTerminate:(UIApplication *)application

  56. {

  57.    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

  58. }

  59. @end


    (2)系统自动找到代理类得didFinishLaunchingWithOption方法,执行。

        所以,有关启动之后最先要做的事情,都写在这个方法中。



5.UIWindow 对象

    (1)什么是window对象?

        应用程序为了显示出界面而准备的一个基础对象,整个应用程序都是在一个window对象上进行表现的。

    (2)window对对象的作用:作为根试图容器来承载界面上要显示的一些元素。


6.frame属性

    (1)是什么?

        一种CGRect类型的结构体,用于描述子视图左顶点在父视图坐标系下的位置,以及子视图在父视图中占据的大小。

    (2)包含的部分:

  1. struct CGRect {

  2.  CGPoint origin;

  3.  CGSize size;

  4. };

  5. typedef struct CGRect CGRect;

  1. struct CGPoint {

  2.  CGFloat x;

  3.  CGFloat y;

  4. };

  5. typedef struct CGPoint CGPoint;

  1. struct CGSize {

  2.  CGFloat width;

  3.  CGFloat height;

  4. };

  5. typedef struct CGSize CGSize;


    (3)如何创建CGRect类型的结构体的变量呢?

    借助于系统提供的一组全局函数,这组函数的名称很有特点,在要创建的类型后加make.

        CGRect     ———> CGRectMake(x,y,width,height);

        CGSize     ———> CGSizeMake(width,heiht);

        CGPoint   ———> CGPointMake(x,y);





7.iOS应用程序的结构设计理念

    (1)控制器+视图

    (2)严格区分显示与控制这两部分功能,视图负责外观,控制器负责对视图的管理以及对用户操作的响应。



    (3)控制器:UIViewController

            视图:UIView

            UIVIew:是界面上一切可见元素的父类,用于描述界面上占据的一块矩形区域,还可以实现嵌套。

            UIViewController:看不见的,但是能够对视图展示的外观,与用户交互的响应提供实现。


    (4)两者之间的关系L系统定义的UIViewController天生自带了一个UIView类型的视图,通过控制器的view属性来访问自带的这个视图。可以理解为,只要创建了一个控制器的实力,同时就创建了一个空白的视图界面,然后,视图上有什么内容,元素的位置,与用户的交互都由控制器来完成。


8.ViewController中的viewDidLoad方法

    (1)是什么?

        是控制器对它自带的那个视图的管理的生命周期的第一阶段。

    (2)作用是什么?

        负责创建视图内容,定制界面显示。

    (3)注意事项:

        只要控制器对象没有被销毁过,则该方法只会执行一次,方法中的第一句 super。。。要保留,意味着父类中做过的有关界面初始化的设计要保留。



9.控件和视图

    (1)什么是视图?

        有外观的,看的见的都可以统称为视图。

        (UIView)

    (2)什么是控件?

        具备针对用户操作提供响应的视图。

        (UIControl)

    (3)视图:

        强调的是一个范围大一点,起到容纳其他视图的容器。

    (4)控件:

        强调的是一个范围大一点,起到响应用户请求的视图。

    (5)严格意义上

        UILabel不算控件,只是交流时回习惯性形容为标签控件。



10.UILabel

    (1)是什么?

        显示文字信息的一种界面视图。

    (2)常用属性:

        .text    用于设定文字内容

        .numberOfLines    用于设定显示的文本的行数(默认为1,修改为0,泽意味行数无上限)

        .font    ​字体

        .textColor    ​字体颜色

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view.
  5. UILabel * label = [[UILabel alloc] init];
  6. label.frame = CGRectMake(50, 50, 200, 60);
  7. label.text = @"这是一段测试的文字看看能否显示的完成马?你说呢?";
  8. label.font = [UIFont systemFontOfSize:24];
  9. // label.textColor = [UIColor greenColor];
  10. label.textColor = [UIColor colorWithRed:1 green:0.5 blue:0.5 alpha:1];
  11. label.numberOfLines = 0;
  12. label.backgroundColor = [UIColor grayColor];
  13. // 设置换行后,切割字符串的顺序
  14. label.lineBreakMode = NSLineBreakByTruncatingTail;
  15. self.view.backgroundColor = [UIColor whiteColor];
  16. [self.view addSubview:label];
  17. UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(70, 220, 200, 70)];
  18. label2.backgroundColor = [UIColor greenColor];
  19. label2.text = @"Label Text";
  20. label2.textAlignment = NSTextAlignmentCenter;
  21. label2.font = [UIFont boldSystemFontOfSize:20];
  22. [self.view addSubview:label2];
  23. }

11.UIButton

    ​(1)是什么?

    ​    ​可以响应用户点击动作的一种控件

    ​(2)创建方式

    ​    ​    ​使用 buttonWithType的工厂方法

    ​(3)常用方法

    ​    ​    ​setTitle    ​设置按钮不同状态下显示的文字

    ​    ​    ​setBackgroundColor

    (4)事件

    ​    ​touchUpInside    按下释放响应事件



  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view.
  5. // 创建一个按钮
  6. UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  7. // 给按钮设置位置与大小
  8. button.frame = CGRectMake(50, 50, 200, 200);
  9. // 设置按钮普通状态的标题
  10. [button setTitle:@"MyButton" forState:UIControlStateNormal];
  11. // 设置按钮高亮时的标题
  12. [button setTitle:@"MyButton-high" forState:UIControlStateHighlighted];
  13. // 设置背景色
  14. button.backgroundColor = [UIColor greenColor];
  15. // 设置按钮点击响应的事件方法
  16. [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  17. // 将按钮控件添加至该控制器的视图上
  18. [self.view addSubview:button];
  19. }
  20. // 按钮点击的方法
  21. - (void)buttonClick:(id)sender
  22. {
  23. NSLog(@"Button Click -- %@",sender);
  24. }



作业:

1.做一个小练习

    ​界面中有一个按钮,每次按下按钮,就面上会多一个label

    ​要求:

    ​a.label之间间隔10个点距离

    ​b.所有label和屏幕左边距离20格点

    ​c.所有label宽280,高20

    ​d.每个label的内容需要叠加。

    ​e.设置标签的背景色为浅灰色

  1. //
  2. // MyViewController.m
  3. // 5_作业
  4. //
  5. // Created by 万齐鹣 on 15/6/2.
  6. // Copyright (c) 2015年 万齐鹣. All rights reserved.
  7. //
  8. #import "MyViewController.h"
  9. @interface MyViewController ()
  10. @end
  11. @implementation MyViewController
  12. {
  13. int _labelNumber;
  14. NSString * _labelString;
  15. }
  16. - (void)viewDidLoad
  17. {
  18. [super viewDidLoad];
  19. // Do any additional setup after loading the view.
  20. self->_labelNumber = 0;
  21. self->_labelString = @"";
  22. UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  23. button.frame = CGRectMake(100, 10, 100, 40);
  24. [button setTitle:@"Add Label" forState:UIControlStateNormal];
  25. [button addTarget:self action:@selector(addLabel) forControlEvents:UIControlEventTouchUpInside];
  26. //self.view.backgroundColor = [UIColor whiteColor];
  27. [self.view addSubview:button];
  28. }
  29. - (void)addLabel
  30. {
  31. _labelNumber++;
  32. NSMutableString * mString;
  33. if (_labelNumber == 1) {
  34. _labelString = @"Hello";
  35. mString = [_labelString mutableCopy];
  36. }else{
  37. mString = [_labelString mutableCopy];
  38. [mString appendString:@" World"];
  39. _labelString = mString;
  40. }
  41. int labelY = (_labelNumber-1)*30 + 50;
  42. UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, labelY, 280, 20)];
  43. label.text = mString;
  44. label.backgroundColor = [UIColor lightGrayColor];
  45. [self.view addSubview:label];
  46. }
  47. @end




0 0