UI01-UIView(示例代码)

来源:互联网 发布:七日杀不让刷js 编辑:程序博客网 时间:2024/06/05 15:55
#import "AppDelegate.h"@interface AppDelegate ()@property(nonatomic,assign)NSArray *apps;@end@implementation AppDelegate-(void)dealloc{    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.#if 0    //UI添加代码的地方    /*     * 创建UI工程的步骤(Single View)     * 1. 删除ViewController.h 和 .m文件     * 2. 工程文件中 general选项中 删除Main Interface处的Main     * 3. 工程文件中 build setting 选项中 ARC ->MRC     * 4. Applocation.h中 strong -> retain     * 5. Application.m中 添加dealloc方法     * 6. application.m中 创建 UIWindow     */    //创建UIWindow  系统自动触发    // [UIScreen mainScreen].bounds  屏幕大小    // frame  矩形大小    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    //设置颜色    self.window.backgroundColor = [UIColor cyanColor];    //设置为主视图,可见    [self.window makeKeyAndVisible];    //Xcode 7.0 解决崩溃 不允许没有跟视图控制器    self.window.rootViewController = [[UIViewController alloc]init];    //视图类 UIView(视图类基类 BaseClass)    // 1.  创建对象 设置frame属性    UIView *oneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];    // 2. 使用对象 设置属性    [oneView setBackgroundColor:[UIColor yellowColor]];    // 3. 将视图(oneView)添加到某处(window)显示    // oneview 称之为 子试图    // self.window  称之为 父视图    [self.window addSubview:oneView];    // 4. 内存管理    [oneView release];    // 设置属性    oneView.frame = CGRectMake(100, 100, 100, 100);    // 每一个视图对象都有一个子视图数组和父视图    // 子视图数组    // 包含在当前视图上添加所有的子视图对象    NSLog(@"subView: %@ ",self.window.subviews);    // 父视图    NSLog(@" superView: %@ ",oneView.superview);    //打印屏幕尺寸 ()    NSLog(@"%@",NSStringFromCGRect(self.window.frame));    /***   UIView重要属性   ***/    // center 中心点    // CGXXXMake()函数 快速创建对类型的结构体    // center被修改时 视图起始点会发生变化    oneView.center = CGPointMake(100, 100);    /***<    hidden 隐藏/显示    >***/    oneView.hidden = YES;    /***<    alpha 透明度    >***/    // 0.0(透明) -> 1.0(不透明)    oneView.alpha = 1.0;    /***<    tag 标记值    >***/    // 通过标记间接访问对应视图    oneView.tag = 1000;    //根据标记获取对象    UIView *tagView = [self.window viewWithTag:1000];    tagView.backgroundColor = [UIColor whiteColor];    //内存管理    [_window release];#endif    // 练习:    // 1.创建两个视图blueView/redView    // 2.尝试修改各种UIView属性(尝试两个视图相交) 查看变化    // 3.将blueView的父视图由self.window->redView 查看变化    // 4.修改blueView的属性 查看变化    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    self.window.backgroundColor = [UIColor colorWithRed:248/255.0 green:195/255.0 blue:205/255.0 alpha:1];    [self.window makeKeyAndVisible];    self.window.rootViewController = [[UIViewController alloc]init];    /*    UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];    blueView.backgroundColor = [UIColor blueColor];    blueView.hidden = NO;    blueView.alpha = 0.5;    [self.window addSubview:blueView];    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    redView.backgroundColor = [UIColor redColor];    [self.window addSubview:redView];    redView.hidden = NO;    redView.alpha = 0.5;    // 父视图的属性修改 子试图也会跟随变化    // frame 设置时的坐标系 参照物为当然的父视图 坐标原点为父视图的左上角    [redView addSubview:blueView];    [redView release];    [blueView release];     */#if 0    // aView    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    aView.backgroundColor = [UIColor cyanColor];    [self.window addSubview:aView];    [aView release];    //bView    UIView *bView = [[UIView alloc]initWithFrame:aView.frame];    bView.backgroundColor = [UIColor yellowColor];    [self.window addSubview:bView];    [bView release];    //操作层级关系    // 拿到前端 front    //[self.window bringSubviewToFront:aView];    // 放到最后 back    //[self.window sendSubviewToBack:bView];    // 移除视图 remove    //[bView removeFromSuperview];    // 定时器 NSTimer    // 每隔固定时间让某个对象执行某个方法    // 参数1 时间间隔    // 参数2 某个对象    // 参数3 某个方法    // 参数5 是否重复    UIView *temp = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];    temp.backgroundColor = [UIColor whiteColor];    [self.window addSubview:temp];    temp.tag = 1000;    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeat) userInfo:nil repeats:YES];#endif#warning homework-one#if 0    /*    创建5个UIView    要求 :    1.命名 view1 ~ view5    2.分别设置背景颜色红、黄、绿、蓝、灰    3.位置与大小自定义。    4.设置UIView常用属性。(数据自定)    */    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(70, 70, 100, 100)];    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(90, 90, 100, 100)];    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(110, 110, 100, 100)];    UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(130, 130, 100, 100)];    view1.backgroundColor = [UIColor redColor];    view2.backgroundColor = [UIColor yellowColor];    view3.backgroundColor = [UIColor greenColor];    view4.backgroundColor = [UIColor blueColor];    view5.backgroundColor = [UIColor lightGrayColor];    [self.window addSubview:view1];    [self.window addSubview:view2];    [self.window addSubview:view3];    [self.window addSubview:view4];    [self.window addSubview:view5];    view5.alpha = 0.5;    view4.hidden = YES;    view4.center = CGPointMake(150, 150);    [view1 release];    [view2 release];    [view3 release];    [view4 release];    [view5 release];#endif#warning homework-second#if 0    /*****    <使用UIView搭建界面>    *****/    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(40, 60, 80, 40)];    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(40, 130, 80, 40)];    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(160, 60, 120, 40)];    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(160, 130, 120, 40)];    UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(80, 200, 60, 40)];    UIView *view6 = [[UIView alloc]initWithFrame:CGRectMake(170, 200, 60, 40)];    view1.backgroundColor = [UIColor cyanColor];    view2.backgroundColor = [UIColor cyanColor];    view3.backgroundColor = [UIColor cyanColor];    view4.backgroundColor = [UIColor cyanColor];    view5.backgroundColor = [UIColor cyanColor];    view6.backgroundColor = [UIColor cyanColor];    [self.window addSubview:view1];    [self.window addSubview:view2];    [self.window addSubview:view3];    [self.window addSubview:view4];    [self.window addSubview:view5];    [self.window addSubview:view6];    [view6 release];    [view5 release];    [view4 release];    [view3 release];    [view2 release];    [view1 release];#endif#warning homework thrid#if 1    /*****    <使用UIView搭建界面>    *****/    int totalloc=3;     CGFloat appvieww=80;     CGFloat appviewh=90;     CGFloat margin=(self.window.frame.size.width-totalloc*appvieww)/(totalloc+1);     int count=self.apps.count;     for (int i=0; i<count; i++) {         int row=i/totalloc;//行号         //1/3=0,2/3=0,3/3=1;         int loc=i%totalloc;//列号         CGFloat appviewx=margin+(margin+appvieww)*loc;         CGFloat appviewy=margin+(margin+appviewh)*row;        UIView *appview=[[UIView alloc]initWithFrame:CGRectMake(appviewx, appviewy, appvieww, appviewh)];        [appview setBackgroundColor:[UIColor purpleColor]];        [self.window addSubview:appview];                appview.tag = 1000;        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeat) userInfo:nil repeats:YES];         [self.window addSubview:appview];#endif    [_window release];    }    return YES;}-(void)repeat{    UIView *appview = [self.window viewWithTag:1000];    appview.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];    [self.window addSubview:appview];}@end
0 0
原创粉丝点击