day015 ios入门demo

来源:互联网 发布:电话轰炸机原理知乎 编辑:程序博客网 时间:2024/06/10 12:29

demo1,实现功能:创建视图控件,并在背景上移动

//
//  main.m
//  Demo1
//
//  Created by 蔡定龙 on 15-4-1.
//  Copyright (c) 2015年 李灵杰. All rights reserved.
//


#import <UIKit/UIKit.h>
#import "AppDelegate.h"


/*
 应用程序运行起来
 1.创建一个应用程序 UIApplicationMain
 
 UIAPPlicationMain:
    应用程序UI主线程(User Interface)
    将任务委托给AppDelegate
 
 AppDelegate
    每一个应用程序都有一个代理(委托),唯一,
    完成  
         程序启动配置
         界面的搭建
 */
int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}


//
//  AppDelegate.h
//  Demo1
//
//  Created by 蔡定龙 on 15-4-1.
//  Copyright (c) 2015年 李灵杰. All rights reserved.
//


#import <UIKit/UIKit.h>


/*
 响应链:服从应用程序的代理
 由很多响应程序组成的链条叫做响应链
 
 UIWindow:应用程序的窗口
 1.容器
 2.负责时间的传递
 3.处理屏幕旋转
 
 
 一个应用程序一般只有一个UIWindow(窗口)
 
 */
@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;


//
//  AppDelegate.m
//  Demo1
//
//  Created by 蔡定龙 on 15-4-1.
//  Copyright (c) 2015年 李灵杰. All rights reserved.
//


#import "AppDelegate.h"
#import "ViewController.h"


@interface AppDelegate ()
            


@end


@implementation AppDelegate
            


//应用程序运行起来调用的第一个方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //获取设备硬件的屏幕大小
    CGRect rect = [UIScreen  mainScreen].bounds;
    /*
     320*480 iphone 4 4s
     320*568 iphone 5 5s
     375*667 iphone 6
     414*736 iphone 6plus
     */
    
    //创建一个窗口,大小和屏幕大小一样
    self.window = [[UIWindow alloc]initWithFrame:CGRectIntegral(rect)];
    //设置窗口大小可以通过cgrectmake(x,y,width,height)
    //self.window = [[UIWindow alloc]initWithFrame:CGRectMake(20, 34, 54, 65)];
    //给窗口设定颜色
    _window.backgroundColor = [UIColor blueColor];
    
    //为窗口创建一个页面
    ViewController *vc = [[ViewController alloc]init];
    
    //为主窗口设置颜色,如不设置则默认为透明色,界面颜色覆盖窗口颜色
    //vc.view.backgroundColor = [UIColor yellowColor];
    vc.view.alpha = 0.7;
    //把vc设置为窗口的主界面
    _window.rootViewController =vc ;
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // 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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // 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.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // 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.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 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.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end


//
//  ViewController.h
//  Demo1
//
//  Created by 蔡定龙 on 15-4-1.
//  Copyright (c) 2015年 李灵杰. All rights reserved.
//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController
/*
 视图:看得到到所有的UI控件都是视图
      UIView只是视图的一种
 
 给窗口设置一个根视图控制器(rootViewController)
 UIViewController就是一个界面
 */
@property (nonatomic,strong) UIView* redView;


@end


//
//  ViewController.m
//  Demo1
//
//  Created by 蔡定龙 on 15-4-1.
//  Copyright (c) 2015年 李灵杰. All rights reserved.
//


#import "ViewController.h"


@interface ViewController ()
            


@end




@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view setBackgroundColor:[UIColor purpleColor]];
    
   
    
    //如果创建视图UIView对象,创建矩形区域
    CGRect rect = CGRectMake(100, 100, 175, 100);
    self.redView = [[UIView alloc]initWithFrame:rect];
    //透明度0.0-1.0
    
    _redView.alpha=0.9;
    //将超出部分剪掉
    _redView.clipsToBounds = YES;
    //是否响应事件
    _redView.userInteractionEnabled =NO;
    
    //与touch方法设置有关,
    //self.view.userInteractionEnabled= NO;
    
    //背景颜色
    [_redView setBackgroundColor:[UIColor yellowColor]];
    /*将redView添加到当前这个视图上
      redView就是self.view的子视图
      self.view就是redview的父视图
     */
    [self.view addSubview:self.redView];
    
    /*
     frame and bounds
     bounds:相对于自己的坐标位置 x,y都是0
     frame:相对于父视图的坐标位置
     */
    CGRect boundsRect = self.redView.bounds;
    NSLog(@"%.1f,%.1f,%.1f,%.1f",boundsRect.origin.x,boundsRect.origin.y,boundsRect.size.width,boundsRect.size.height);
    CGRect frameRect = self.redView.frame;
    NSLog(@"%.1f,%.1f,%.1f,%.1f",frameRect.origin.x,frameRect.origin.y,frameRect.size.width,frameRect.size.height);
    
    //center 中心点
    //x=100+175/2  y=100+100/2
    NSLog(@"x=%.1f,y=%.1f",self.redView.center.x,self.redView.center.y);
    
    UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 50)];
    greenView.backgroundColor =[UIColor greenColor] ;
    [_redView addSubview:greenView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    self.redView.center = location;
}
@end

效果图:



总结:首先,main函数是一切程序的入口,一个类调用另一个类不要忘记导入头文件

/*
 应用程序运行起来
 1.创建一个应用程序 UIApplicationMain
 
 UIAPPlicationMain:
    应用程序UI主线程(User Interface)
    将任务委托给AppDelegate
 
 AppDelegate
    每一个应用程序都有一个代理(委托),唯一,
    完成  
         程序启动配置
         界面的搭建
 */

 UIWindow作用:应用程序的窗口
 1.容器
 2.负责时间的传递
 3.处理屏幕旋转

/*
 视图:看得到到所有的UI控件都是视图
      UIView只是视图的一种
 
 给窗口设置一个根视图控制器(rootViewController)
 UIViewController就是一个界面
 */

同时,还介绍了许多UIViewController的函数调用方法,同时还介绍了touch函数的用法,用来捕捉用户的操作


demo2   三个不同色块视图,当捕捉到用户该色块操作时变色,用户操作结束后变回原色

//
//  ViewController.m
//  day015 af
//
//  Created by 蔡定龙 on 15-4-3.
//  Copyright (c) 2015年 李灵杰. All rights reserved.
//


#import "ViewController.h"


typedef enum{
    kViewStyleRed,
    kViewStyleBlue,
    kViewStyleYellow,
} kViewStyle;


@interface ViewController ()
@property (nonatomic,strong) NSMutableArray *viewsArray;
            


@end


@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //init array
    self.viewsArray = [NSMutableArray arrayWithCapacity:3];
    
    [self createViewWithFrame:CGRectMake(100, 50, 100, 100) andbgColor:[UIColor redColor] andTag:kViewStyleRed];
    
    [self createViewWithFrame:CGRectMake(100, 200, 100, 100) andbgColor:[UIColor blueColor] andTag:kViewStyleBlue];
    
    [self createViewWithFrame:CGRectMake(100, 350, 100, 100) andbgColor:[UIColor yellowColor] andTag:kViewStyleYellow];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)createViewWithFrame:(CGRect)rect andbgColor:(UIColor *)bgcolor andTag:(kViewStyle)style{
    UIView *tempView = [[UIView alloc]initWithFrame:rect];
    tempView.backgroundColor = bgcolor;
    tempView.tag =style;
    [self.view addSubview:tempView];
    
    [self.viewsArray addObject:tempView];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    //self.view 每块都应该在外层self。view中表示
    CGPoint location = [touch locationInView:self.view];
    for (UIView *tempView in _viewsArray) {
        if (CGRectContainsPoint(tempView.frame, location)) {
            tempView.backgroundColor = [UIColor blackColor];
        }else{
            switch (tempView.tag) {
                case kViewStyleRed:
                    tempView.backgroundColor=[UIColor redColor];
                    break;
                case kViewStyleBlue:
                    tempView.backgroundColor=[UIColor blueColor];
                    break;
                case kViewStyleYellow:
                    tempView.backgroundColor=[UIColor yellowColor];
                    break;
            }
        }
    }
    
}
@end

效果图:




特别注意,尽量将同一方法封装使用,如创建色块,同时要灵活应用array,枚举类型

0 0