iOS规范及语法

//我们编写代码的规范:一定要遵循:驼峰命名法
//1,在起工程名的时候,工程名首字母大写并且接下来的每一个单词的首字母都要大写
//2,在起类名的时候同上
//3,起属性名和起方法名的时候首字母小写接下来的每个单词的首字母都大写
条件判断语句
<#condition#> 条件表达式 只能为真假 条件表达式里面的= 要连写两个
= 表示赋值 把等于好后面的值赋给前面
== 表示判断 是否相等
<#statements#> 陈述 就是代码块执行的地方
if 语句
1. 第一种结构
if为真 if下面的代码块就会被执行 否则 不执行
if()
{}
2.第二种结构
如果if为真 else 不再执行 否则 else 一定会被执行
if()
{}
else
{}
3.第三种结构
所有的条件顺次判断 找一个真的执行 其他的都不会被执行,如果没有真的,执行else里面的代码块
if()
{}
else if()
{}

else
{}
4.第四种结构
if()
代码1;
代码2;
九大基本数据类型
// 属性声明区
// OC中的九大基本数据类型

// 1G = 1024M 1M = 1024K 1K=1024B 1B = 8bit(位)
// 二进制:只有0和1 机器语言
// 0000 0001 存储在硬盘上的一个字节的1 0000 0010

// 7 0000 0111 == 1*2^0+1*2^1+1*2^2 = 7

// 1,整型,整数
// short a =4;存储方式
// 00000000 00000100
// short 短整型 占2个字节 16位 0-2^16-1 占位符 %d
// int 整型 占4个字节 32位 0-2^32-1 占位符 %d
// long 长整型 在32位系统中占4个字节,64位系统占8个字节 占位符 %ld
// long long 长长整型 占8个字节 %lld
// 2,浮点型:(小数)
// float 单精度浮点型 占4个字节 占位符%f
// double 双精度浮点型 占8个字节 占位符%lf

// BOOL 布尔类型 占1个字节(yes(1)/no(0)) 占位符 %d
// char 字符类型 占一个字节 占位符 %c(输出的当前的字符)或者%d(输出的是当前字符的asscii码)

// Byte:字节 占一个字节 占位符%d

// NSString 字符串 占位符 %@
两种属性的声明方式 及打点调用
//第一种属性的声明方式
1 .h
{
float height;
float weight;
}
-(void)setHeight:(float)aHeight;

2.m
-(void)setHeight:(float)aHeight
{
height = aHeight +100;

}
3 main.m

import “People.h”

{
People * p =[People alloc];

[p setHeight:80];

float peopleHeight = [p height];

NSLog(@"peopleHeight====%f",peopleHeight);}

加号方法:即类方法,用类名调用[People eat]
减号方法:实例化方法,先将类进行实例化对象 ,在进行调用[p eat]
第二种属性的声明方式 系统会自为我们生成set get 方法
1 .h
@property NSString * name;
@property int age;
2 .m

import “People.h”

-(float)height;
-(float)height
{
return height;
}
3 main.m

[p setName:@”共和国”];
//
//
// NSLog(@”aaaa======%f”,p.height);
//
//
// [p setAge:18];
// //p.age = [p age]
// NSLog(@”这个小伙子今年%d”,p.age);
}
继承
1 People.h
@property NSString * name;
@property int age;

-(void)work;
2 People.m
@synthesize name;
@synthesize age;

-(void)work
{
NSLog(@”今天是2015年7月29,我和第55期的同学在Happy”);
}
3main
{
People * p = [People alloc];
p.age = 18;
p.name = @”李斯”;
[m work];}
4 建立子类Man 其中Man的超类是People
Man.h
@property NSString *secondName;

-(void)play;
5// Man.m
@synthesize secondName;
-(void)work
{
[super work];
NSLog(@”每个月都有30天不想工作”);
}
6

import “Man.h”

Man * m = [Man alloc];
[m play];

// NSLog(@”secondName====%@”,[m secondName]);

继承:
1.子类可以拥有父类的属性和方法
2.子类可以重写父类的方法
3.子类可以在父类的基础上添加属性或者方法
继承 指针
父类的指针指向一个对象 但是该对象却是子类创建的
这样的话 不会警告,也不会抱错,但是如果调用方法的话,如果该方法是继承自父类的话,会执行子类同名的方法,如果该方法是子类特有的方法,该方法不能被实现
*/
People * p = [Man alloc];

[p work];
     子类的指针指向一个对象 该对象由父类创建
 这样的话 首先会给一个警告 告诉你指针类型和后面的类型不匹配
 调用方法  如果是继承自父类的方法,会执行父类的方法,否则的话,虽然会执行,但是程序会崩溃
Man * m = [People alloc];

[m work];

[m play];

///id 关键字 代表任意一种返回类型
初始化方法的固定格式
1.init开头
2.后面跟一个With
3.初始化方法返回类型为id
1 People.h
@property NSString * name;
@property int age;
-(id)initWithName:(NSString *)aName age:(int)aAge;
2 People.m
@synthesize name;
@synthesize age;
-(id)initWithName:(NSString *)aName age:(int)aAge
{
self = [super init];
if (self)
{
name = aName;
age = aAge;
}

return self;

}
3 main
People * t = [[People alloc] initWithName:@”那个啥” age:18];
NSLog(@”name=======%@”,t.name);
4 建立Man Women类,都继承与People类
main 初始化的时候赋值
#import “Man.h”
#import “Woman.h”
{People * t = [[People alloc] initWithName:@”那个啥” age:18];
NSLog(@”name=======%@”,t.name);

Man *m = [[Man alloc] initWithName:@"小土豆" age:22];
NSLog(@"name=%@,age=%d",m.name,m.age);

Woman * w = [[Woman alloc] initWithName:@"手机" age:123];

NSLog(@"wName===%@,wAge===%d",w.name,w.age);

}
创建一个电脑类
1 电脑拥有不同的属性:价格 品牌 生产日期 颜色 内存 硬盘
2 创建不同的方法设置属性
2.1分别写出几个属性的set get方法
2.2 写一个多参数的方法 调用此方法 一次性设置所有属性的值
2.3 理解并能熟练运用今天所讲的各种方法
set方法
1 Computer.h
{
int price;
NSString * brand;
NSString * color;
}
-(void)setPrice:(int)aPrice;

2 Computer.m
-(void)setPrice:(int)aPrice
{ price += aPrice;

NSLog(@"price====%d",price);

}
3 main.m

import “Computer.h”

{ Computer * c = [Computer alloc];
[c setPrice:4001];
get 方法 仿照系统的写法
1 Computer.h
{
int price;
}
-(int)price;
2 Computer.m
-(int)price
{
NSLog(@”get=====%d”,price);return price;
}
3 main.m
int computerPrice = [c price];

if (computerPrice > 4000)
{
    NSLog(@"电脑太贵了,没钱");
}

打点调用
(1) float peopleHeight = [p height];

NSLog(@"peopleHeight====%f",peopleHeight);

(2)NSLog(@”peopleHeight====%f”,p.height);
初始化
1 People.h
@property NSString * name;
@property int age;

//id 关键字 代表任意一种返回类型
/*
初始化方法的固定格式
1.init开头
2.后面跟一个With
3.初始化方法返回类型为id
*/
-(id)initWithName:(NSString *)aName age:(int)aAge;
2 People.m
@synthesize name;
@synthesize age;

-(id)initWithName:(NSString *)aName age:(int)aAge
{
//self 自己
self = [super init];

//相当于 self != nil;
//if(self)  如果self为真  就是说调用父类初始化方法成功
if (self)
{
    name = aName;
    age  = aAge;
}

return self;

}
3 main.m

import “Man.h”

import “Woman.h”

{
//没有初始化 分配空间
People * a = [People alloc];
NSLog(@”a.name=====%d”,a.age);

//init 初始化  但是没有赋值
People * p = [[People alloc] init];
p.name = @"fsdf";
p.age = 23;
NSLog(@"p.name ======%d",p.age);

//初始化的时候赋值
People * t = [[People alloc] initWithName:@"那个啥" age:18];
NSLog(@"name=======%@",t.name);

Man *m = [[Man alloc] initWithName:@"小土豆" age:22];
NSLog(@"name=%@,age=%d",m.name,m.age);

Woman * w = [[Woman alloc] initWithName:@"手机" age:123];

NSLog(@"wName===%@,wAge===%d",w.name,w.age);

}
//bool值 no=0,yes=1 默认值为0
[其中man woman继承自people]
继承例子:相亲(李晨和范冰冰)
三大控件
(固定格式)
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];
Label TextField Button
//设置Label尺寸
UILabel * nameLable = [[UILabel alloc] initWithFrame:CGRectMake(80, 100, 120, 100)];
//设置背景颜色
[nameLable setBackgroundColor:[UIColor redColor]];
//设置文本信息
[nameLable setText:@”Hello Word”];
//设置居中对齐
[nameLable setTextAlignment:NSTextAlignmentCenter];
//设置字体颜色
[nameLable setTextColor:[UIColor blueColor]];
//设置字体大小
[nameLable setFont:[UIFont systemFontOfSize:30]];
//设置多行显示
[nameLable setNumberOfLines:0];
NSLog(@”font—%@”,[UIFont familyNames]);
//设置字体样式和大小
[nameLable setFont:[UIFont fontWithName:@”DIN Alternate” size:20]];
//增加控件到视图上
[self.window addSubview:nameLable];

//初始化并设置大小
UITextField * nameFiled = [[UITextField alloc] initWithFrame:CGRectMake(60, 100, 200, 30)];
//设置边框
[nameFiled setBorderStyle:UITextBorderStyleRoundedRect];
//设置占位符
[nameFiled setPlaceholder:@”请输入您的姓名”];
//设置文本
[nameFiled setText:@”今天天气真热,橙色预警”];
//设置字体居中对齐
[nameFiled setTextAlignment:NSTextAlignmentCenter];
//设置键盘样式
[nameFiled setKeyboardType:UIKeyboardTypeNumberPad];
//添加到window上
[self.window addSubview:nameFiled];
// 当你的手指接触到屏幕的时候 调用的方法
-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
NSLog(@”触碰”);
//endEditing 结束编辑
[self.window endEditing:YES];
}
//手指滑动取消的时候 调用这个方法
-(void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event
{
NSLog(@”取消”);
}
//手指滑动结束的时候 调用的方法
-(void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
{
NSLog(@”结束”);
}
//手指移动的时候 调用的方法
-(void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
{
NSLog(@”移动”);
}

//创建一个实例化对象 并且设置它的风格样式
UIButton * clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置背景色
[clickBtn setBackgroundColor:[UIColor redColor]];

// 设置大小
[clickBtn setFrame:CGRectMake(100, 100, 100, 100)];
//设置标题 State状态
[clickBtn setTitle:@”登陆” forState:UIControlStateNormal];
//设置标题颜色
[clickBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//设置按钮图片
[clickBtn setImage:[UIImage imageNamed:@”4.png”] forState:UIControlStateNormal];
// addTarget 目标 事件的接受者
// action 行动
// forControlEvents 在什么情况下 会触发此方法
[clickBtn addTarget:self action:@selector(clickToLogin) forControlEvents:UIControlEventTouchUpInside];
[clickBtn addTarget:self action:@selector(clickToRegister) forControlEvents:UIControlEventTouchDown];
[self.window addSubview:clickBtn];
-(void)clickToLogin
{
NSLog(@”Touch Up Inside”);
}
-(void)clickToRegister
{
NSLog(@”Touch Down”);
}
数值转换
// 想要把字符串转成int 被转的对象一定要是一个字符串
int a = [nameFiled.text intValue];
int a = [nameLable.text intValue];
// 把字符串转成float
float a = [nameFiled.text floatValue];
//把float 转成字符串
NSString *b = [NSString stringWithFormat:@“%f”,height];
总体变量
我上面的控件在下面打不出名字
有可能的原因 1.我打的名字拼写错误 导致打不出来
2.我想要操作的对象为局部对象 只在创建改对象的方法内部有效,这时就要在.h中创建总体变量,使得局部变量在总体中能够应用
//两个textField中的文本在label中一起显示出来
showLable.text = [NSString stringWithFormat:@”%@%@”,userFiled.text,passWordFiled.text];
//float型的fund在textField中显示出来,跟随显示
fundText.text = [NSString stringWithFormat:@”%f”,fund];

//把两个方法同时添加到一个视图上
[clickBtn addTarget:self action:@selector(doLogin) forControlEvents:UIControlEventTouchUpInside];
[clickBtn addTarget:self action:@selector(doLogin2) forControlEvents:UIControlEventTouchUpInside];

-(void)doLogin
{
NSLog(@”我现在登录”);
}
-(void)doLogin2
{
NSLog(@”第二个方法”);
}
//第一种 依据坐标
// float y = xiaoGou.frame.origin.y;
// if (y == 100)
// {
// NSLog(@”登录”);
// }
// else if (y == 200)
// {
// NSLog(@”chifan”);
// }
//第二种 依据名字的不同
// NSString *name = xiaoGou.titleLabel.text;
//
// if ([name isEqualToString:@”登录”])
// {
// NSLog(@”登录”);
// }
// else if ([name isEqualToString:@”注册”])
// {
// NSLog(@”休息”);
// }
一个按钮触发多个事件
[loginBtn setTag:1];
[loginBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:loginBtn];

[registBtn setTag:2];
[registBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:registBtn];

if (xiaoGou.tag == 1)
{
NSLog(@”登录”);
}
else
{
NSLog(@”注册”);
}
// 新建一个View 大小等于屏幕的大小 并且label置于backview之上 点击移动按钮的时候backview移动
backView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[backView setBackgroundColor:[UIColor blueColor]];
[self.window addSubview:backView];
UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 100, 100, 30)];
[firstLabel setText:@”第一个”];
[backView addSubview:firstLabel];

UILabel  *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 100, 30)];
[secondLabel setText:@"第二个"];
[secondLabel setBackgroundColor:[UIColor redColor]];
[backView addSubview:secondLabel];

UILabel  *thirdLable = [[UILabel alloc] initWithFrame:CGRectMake(200, 300, 100, 30)];
[thirdLable setText:@"第三个"];
[backView addSubview:thirdLable];


UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"移动" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(200, 350, 100, 30)];
[btn addTarget:self action:@selector(clickToMove) forControlEvents:UIControlEventTouchUpInside];
[backView addSubview:btn];

-(void)clickToMove
{
// 求屏幕的宽高
NSLog(@”———-%f”,backView.frame.size.height);
[backView setFrame:CGRectMake(-20, 0, 320, 568)];
}
//创建一个不可变数组
NSArray *array = [[NSArray alloc] init];
NSArray *array1 = [NSArray array];
//不可变数组 添加元素 每个元素之间,隔开 最后以nil结尾
NSArray *array3 = [NSArray arrayWithObjects:@”小狗”,@”小鸭”,@”小三”, nil];

NSLog(@"count-----%d",array3.count);
//objectAtIndex    获取指定索引的元素
NSLog(@"-------%@",[array3 objectAtIndex:0]);
//indexOfObject   根据元素内容  获得相应的索引值
NSLog(@"------%d",[array3 indexOfObject:@"小鸭"]);


//创建一个可变的数组
NSMutableArray  * mutableArray = [NSMutableArray array];
NSMutableArray  * mutable1 = [NSMutableArray arrayWithCapacity:0];
//增加元素
[mutableArray addObject:@"李晨"];
[mutableArray addObject:@"范冰冰"];
[mutableArray addObject:@"李新"];
[mutableArray addObject:@"凤姐"];



//增加元素到制定位置
[mutableArray insertObject:@"蜡笔小新" atIndex:0];

//移除最后一个元素
[mutableArray removeLastObject];

//移除指定索引的元素
[mutableArray removeObjectAtIndex:2];
//用特定元素来替换制定 位置的元素
[mutableArray replaceObjectAtIndex:0 withObject:@"章子怡"];
//添加另外一个数组中的所有元素到这个数组中
[mutableArray addObjectsFromArray:array3];


//移除所有对象
[mutableArray removeAllObjects];

NSLog(@"--------%d",[mutableArray count]);

UIView * viewUp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[viewUp setBackgroundColor:[UIColor redColor]];
[self.window addSubview:viewUp];

UIView * viewDown = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
[viewDown   setBackgroundColor:[UIColor blueColor]];
[self.window addSubview:viewDown];

// 两个子视图的位置变换
UIView *viewUp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

[viewUp setBackgroundColor:[UIColor redColor]];
[self.window addSubview:viewUp];

UIView *viewDown  = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[viewDown setBackgroundColor:[UIColor blueColor]];
[self.window addSubview:viewDown];

//交换两个视图的位置
[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[self.window insertSubview:twoView aboveSubview:oneView];
[self.window addSubview:twoView];
[self.window insertSubview:oneView belowSubview:twoView];
[self.window insertSubview:twoView aboveSubview:oneView];
[self.window bringSubviewToFront:twoView];
[self.window sendSubviewToBack:oneView];

//交换指定索引视图的位置
// [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//从父视图中移走本身
// [viewDown removeFromSuperview];
//把指定的视图插入指定的位置
// [self.window insertSubview:viewUp atIndex:1];
//把指定的视图置于另一个指定的视图之上
// [self.window insertSubview:viewUp aboveSubview:viewDown];
//把指定的视图放在最上层
//[self.window bringSubviewToFront:viewUp];
// 把指定的视图放置在最下层
// [self.window sendSubviewToBack:viewDown];
touch down点下的时候调用
touch up inside 手指松开的时候调用
for循环 里边有三个条件
1.初始化一个变量<#initialization#>
2.增加一个限制条件<#condition#>
3.递增<#increment#>

NSMutableArray *fireArray = [NSMutableArray array];

for (int i = 1; i <= 17; i ++)
{
//以固定格式得到一张图片
NSString *fireString =[NSString stringWithFormat:@”Fire%d.gif”,i];
NSLog(@”——%@”,fireString);

    //获取图片
    UIImage * fireImage = [UIImage imageNamed:fireString];
    //把图片添加到数组中
    [fireArray addObject:fireImage];
    NSLog(@"count----%d",[fireArray count]);
}

UIImageView * imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//设置一张图片   [imageView setImage:[UIImage imageNamed:@"angry_00.jpg"]];
UIImage *imageName = [UIImage imageNamed:@"Fire1.gif"];
[imageView setImage:imageName];
//设置动画源
[imageView setAnimationImages:fireArray];
//动画持续时间
[imageView setAnimationDuration:1.7];
//循环次数  0代表无限
[imageView setAnimationRepeatCount:0];



[self.window addSubview:imageView];

//开始动画
[imageView startAnimating];

雪花

snowArray = [NSMutableArray array];
//使用for循环一次性创建多个雪花视图
for (int i = 1; i <= 60; i++)
{

    UIImageView * snowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, -40, 40, 40)];
    //增加图片到UIimageView上
    [snowImageView setImage:[UIImage imageNamed:@"flake.png"]];
    //我们通过一个tag值0来代表它的使用状态  未使用
    [snowImageView setTag:0];
    //添加到视图

    [self.view addSubview:snowImageView];

    [snowArray addObject:snowImageView];


}

//创建一个定时器  每隔一段时间找到一个snow
//scheduledTimerWithTimeInterval  时间间隔  就是说每隔多长时间调用一次定时器 的方法
//userInfo 问你有没有什么想要给它什么值
//repeats  重复  就是说 你这个定时器 想要执行一次  还是多次执行
[NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];

}

-(void)timerRun
{
//遍历数组找雪花 找到一个使用的雪花以后就不再着了
for (int i = 1; i <=[snowArray count]; i++)
{
//snowArray[i] 获取数组中当前索引为i的这个元素
UIImageView *downImageView = snowArray[i];
//如果tag值为0的话 说明这个雪花没有被使用 你现在可以使用
if (downImageView.tag == 0)
{
//我们让1来代表使用的状态
downImageView.tag = 1;
//找到未使用的雪花后 给它一个随机的位置
[downImageView setFrame:CGRectMake(arc4random()%280, -40, 40, 40)];
/*
animateWithDuration 动画持续时间
*/
//做一个下落的动画
[UIView animateWithDuration:10 animations:^
{
//雪花最终要到达的地方
[downImageView setFrame:CGRectMake(arc4random()%280, 480,40, 40)];
//动画完成后 更改使用状态和坐标
} completion:^(BOOL finished)
{
downImageView.tag = 0;
//雪花完成结束后回到的地方
[downImageView setFrame:CGRectMake(0, -40, 40, 40)];

         }];
        return;
    }
}

}
汤姆猫

@interface ZYYYViewController ()
{
NSMutableArray * cymbalArray;
NSMutableArray * drinkArray;
NSMutableArray * eatArray;
NSMutableArray * fartArray;
NSMutableArray * pieArray;
NSMutableArray * scratchArray;
NSMutableArray * footLeftArray;
NSMutableArray * footRightArray;
NSMutableArray * angryArray;
NSMutableArray * knockoutArray;
NSMutableArray * stomachArray;

UIImageView * cymbalView;
UIImageView * tomCatImageView;
UIImageView * drinkView;
UIImageView * eatView;
UIImageView * fartView;
UIImageView * pieView;
UIImageView * scratchView;
UIImageView * footLeftView;
UIImageView * footRightView;
UIImageView * angryView;
UIImageView * knockoutView;
UIImageView * stomachView;

}

@end

@implementation ZYYYViewController

  • (id)initWithNibName:(NSString )nibNameOrNil bundle:(NSBundle )nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor yellowColor]];

    //汤姆猫视图
    tomCatImageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    tomCatImageView.userInteractionEnabled = YES;
    [tomCatImageView setImage:[UIImage imageNamed:@”angry_00.jpg”]];
    [self.view addSubview:tomCatImageView];

    //得到13张cymbal
    cymbalArray = [NSMutableArray array];
    for (int i = 0; i <= 12; i++)
    {
    NSString * cymbalString =[NSString stringWithFormat:@”cymbal_%d.jpg”,i];
    UIImage * cymbalImage =[UIImage imageNamed:cymbalString];
    [cymbalArray addObject:cymbalImage];
    }
    //cymbal 按钮
    UIButton * cymbalBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 200, 50,50)];
    [cymbalBtn setImage:[UIImage imageNamed:@”cynbals.png”] forState:UIControlStateNormal];
    [cymbalBtn setTag:1];
    [cymbalBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:cymbalBtn];

    //得到81张drink图片
    drinkArray = [NSMutableArray array];
    for (int i = 0; i <= 80; i++)
    {
    NSString * drinkString = [NSString stringWithFormat:@”drink_%02d.jpg”,i];

    UIImage * drinkImage = [UIImage imageNamed:drinkString];
    [drinkArray addObject:drinkImage];
    

    }

    UIButton * drinkBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 250, 50, 50)];
    [drinkBtn setImage:[UIImage imageNamed:@”milk.png”] forState:UIControlStateNormal];
    [drinkBtn setTag:2];
    [drinkBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:drinkBtn];

    //得到31张eat 图片
    eatArray = [NSMutableArray array];
    for (int i = 0; i <= 30; i++)
    {
    NSString * eatString = [NSString stringWithFormat:@”eat_%02d.jpg”,i];
    UIImage * eatImage = [UIImage imageNamed:eatString];
    [eatArray addObject:eatImage];
    }
    UIButton * eatBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 300, 50, 50)];
    [eatBtn setImage:[UIImage imageNamed:@”larry.png”] forState:UIControlStateNormal];
    [eatBtn setTag:3];
    [eatBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:eatBtn];

    //得到28张fart的图片
    fartArray = [NSMutableArray array];
    for (int i = 0; i <= 27; i++)
    {
    NSString * fartString= [NSString stringWithFormat:@”fart_%02d.jpg”,i];
    UIImage * fartImage = [UIImage imageNamed:fartString];
    [fartArray addObject:fartImage];
    }
    UIButton * fartBtn = [[UIButton alloc]initWithFrame:CGRectMake(250, 200, 50, 50)];
    [fartBtn setImage:[UIImage imageNamed:@”fart.png”] forState:UIControlStateNormal];
    [fartBtn setTag:4];
    [fartBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:fartBtn];
    //得到pie 24张图片
    pieArray = [NSMutableArray array];
    for (int i = 0; i<= 23; i++)
    {
    NSString * pieString = [NSString stringWithFormat:@”pie_%02d.jpg”,i];
    UIImage * pieImage = [UIImage imageNamed:pieString];
    [pieArray addObject:pieImage];
    }
    UIButton * pieBtn = [[UIButton alloc]initWithFrame:CGRectMake(250, 250, 50, 50)];
    [pieBtn setImage:[UIImage imageNamed:@”pie.png”] forState:UIControlStateNormal];
    [pieBtn setTag:5];
    [pieBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:pieBtn];
    //得到scratch 56张图片
    scratchArray = [NSMutableArray array];
    for (int i = 0; i<= 55; i++)
    {
    NSString * scratchString = [NSString stringWithFormat:@”scratch_%02d.jpg”,i];
    UIImage * scratchImage = [UIImage imageNamed:scratchString];
    [scratchArray addObject:scratchImage];
    }
    UIButton * scratchBtn = [[UIButton alloc]initWithFrame:CGRectMake(250, 300, 50, 50)];
    [scratchBtn setImage:[UIImage imageNamed:@”pawn.png”] forState:UIControlStateNormal];
    [scratchBtn setTag:6];
    [scratchBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:scratchBtn];
    //左脚
    footLeftArray = [NSMutableArray array];
    for (int i = 0; i<= 29; i++)
    {
    NSString * footLeftString = [NSString stringWithFormat:@”foot_Left_%02d.jpg”,i];
    UIImage * footLeftImage = [UIImage imageNamed:footLeftString];
    [footLeftArray addObject:footLeftImage];
    }
    UIButton * footLeftBtn = [[UIButton alloc]initWithFrame:CGRectMake(150, 400, 50, 50)];
    //[footLeftBtn setBackgroundColor:[UIColor blueColor]];
    [footLeftBtn setTag:7];
    [footLeftBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:footLeftBtn];
    //右脚
    footRightArray = [NSMutableArray array];
    for (int i = 0; i<= 29; i++)
    {
    NSString * footRightString = [NSString stringWithFormat:@”foot_right_%02d.jpg”,i];
    UIImage * footRightImage = [UIImage imageNamed:footRightString];
    [footRightArray addObject:footRightImage];
    }
    UIButton * footRightBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 400, 50, 50)];
    //[footRightBtn setBackgroundColor:[UIColor blueColor]];
    [footRightBtn setTag:8];
    [footRightBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:footRightBtn];
    //击倒
    knockoutArray = [NSMutableArray array];
    for (int i = 0; i<= 80; i++)
    {
    NSString * knockoutString = [NSString stringWithFormat:@”knockout_%02d.jpg”,i];
    UIImage * knockoutImage = [UIImage imageNamed:knockoutString];
    [knockoutArray addObject:knockoutImage];
    }
    UIButton * knockoutBtn = [[UIButton alloc]initWithFrame:CGRectMake(130, 100, 50, 50)];
    //[knockoutBtn setBackgroundColor:[UIColor blueColor]];
    [knockoutBtn setTag:9];
    [knockoutBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:knockoutBtn];
    //胃
    stomachArray = [NSMutableArray array];
    for (int i = 0; i<= 33; i++)
    {
    NSString * stomachString = [NSString stringWithFormat:@”stomach_%02d.jpg”,i];
    UIImage * stomachImage = [UIImage imageNamed:stomachString];
    [stomachArray addObject:stomachImage];
    }
    UIButton * stomachBtn = [[UIButton alloc]initWithFrame:CGRectMake(130, 270, 50, 50)];
    //[stomachBtn setBackgroundColor:[UIColor blueColor]];
    [stomachBtn setTag:10];
    [stomachBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:stomachBtn];

    angryArray = [NSMutableArray array];
    for (int i = 0; i<= 25; i++)
    {
    NSString * angryString = [NSString stringWithFormat:@”angry_%02d.jpg”,i];
    UIImage * angryImage = [UIImage imageNamed:angryString];
    [angryArray addObject:angryImage];
    }
    UIButton * angryBtn = [[UIButton alloc]initWithFrame:CGRectMake(130, 200, 50, 50)];
    //[angryBtn setBackgroundColor:[UIColor blueColor]];
    [angryBtn setTag:11];
    [angryBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [tomCatImageView addSubview:angryBtn];

    UIButton * zyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [zyBtn setFrame:CGRectMake(0, 400, 100, 100)];
    [zyBtn setBackgroundColor:[UIColor clearColor]];
    [zyBtn setTitle:@”Snow” forState:UIControlStateNormal];
    [zyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [zyBtn addTarget:self action:@selector(clickToZy) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zyBtn];

    UIButton * zyyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [zyyBtn setFrame:CGRectMake(100, 400, 100, 100)];
    [zyyBtn setBackgroundColor:[UIColor clearColor]];
    [zyyBtn setTitle:@”Fire” forState:UIControlStateNormal];
    [zyyBtn addTarget:self action:@selector(clickToZyy) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zyyBtn];

    UIButton * zyyy = [UIButton buttonWithType:UIButtonTypeSystem];
    [zyyy setFrame:CGRectMake(200, 400, 120, 100)];
    [zyyy setBackgroundColor:[UIColor clearColor]];
    [zyyy setTitle:@”TomCat” forState:UIControlStateNormal];
    [zyyy setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [zyyy addTarget:self action:@selector(clickToZyyy) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zyyy];

}
-(void)click:(UIButton *)btn
{
if (btn.tag == 1)
{
//把图片添加到视图上并设置动画
cymbalView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[cymbalView setAnimationImages:cymbalArray];
[cymbalView setAnimationDuration:1.3];
[cymbalView setAnimationRepeatCount:1];
[self.view addSubview:cymbalView];
[cymbalView startAnimating];
return;
}
else if (btn.tag ==2 )
{
drinkView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[drinkView setAnimationImages:drinkArray];
[drinkView setAnimationDuration:8.1];
[drinkView setAnimationRepeatCount:1];
[self.view addSubview:drinkView];
[drinkView startAnimating];
return;
}
else if (btn.tag == 3)
{
eatView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[eatView setAnimationImages:eatArray];
[eatView setAnimationDuration:3.1];
[eatView setAnimationRepeatCount:1];
[self.view addSubview:eatView];
[eatView startAnimating];
return;
}
else if (btn.tag == 4)
{
fartView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[fartView setAnimationImages:fartArray];
[fartView setAnimationDuration:2.8];
[fartView setAnimationRepeatCount:1];
[self.view addSubview:fartView];
[fartView startAnimating];
return;
}
else if(btn.tag == 5)
{
pieView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[pieView setAnimationImages:pieArray];
[pieView setAnimationDuration:2.4];
[pieView setAnimationRepeatCount:1];
[self.view addSubview:pieView];
[pieView startAnimating];
return;
}
else if (btn.tag == 6)
{
scratchView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[scratchView setAnimationImages:scratchArray];
[scratchView setAnimationDuration:5.6];
[scratchView setAnimationRepeatCount:1];
[self.view addSubview:scratchView];
[scratchView startAnimating];
return;
}
else if (btn.tag == 7)
{
footLeftView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[footLeftView setAnimationImages:footLeftArray];
[footLeftView setAnimationDuration:3];
[footLeftView setAnimationRepeatCount:1];
[self.view addSubview:footLeftView];
[footLeftView startAnimating];
return;
}
else if (btn.tag == 8)
{
footRightView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[footRightView setAnimationImages:footRightArray];
[footRightView setAnimationDuration:3];
[footRightView setAnimationRepeatCount:1];
[self.view addSubview:footRightView];
[footRightView startAnimating];
return;
}
else if(btn.tag == 9)
{
knockoutView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[knockoutView setAnimationImages:knockoutArray];
[knockoutView setAnimationDuration:8.1];
[knockoutView setAnimationRepeatCount:1];
[self.view addSubview:knockoutView];
[knockoutView startAnimating];
return;

}
if (btn.tag == 10)
{
    stomachView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [stomachView setAnimationImages:stomachArray];
    [stomachView setAnimationDuration:3.4];
    [stomachView setAnimationRepeatCount:1];
    [self.view addSubview:stomachView];
    [stomachView startAnimating];
    return;
}
else if (btn.tag == 11)
{
    angryView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [angryView setAnimationImages:angryArray];
    [angryView setAnimationDuration:2.6];
    [angryView setAnimationRepeatCount:1];
    [self.view addSubview:angryView];
    [angryView startAnimating];
    return;

}

}
数组
NSArray *tempArray = [NSArray arrayWithObjects:@”小红”,@”小明”,@”小蓝”, nil];

NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
[mutableArray addObject:@"1"];
[mutableArray addObject:@"2"];
[mutableArray addObject:@"3"];


[mutableArray removeObjectAtIndex:0];

[mutableArray addObject:@"1000"];
[mutableArray insertObject:@"88" atIndex:0];
[mutableArray replaceObjectAtIndex:0 withObject:@"213"];
[mutableArray removeLastObject];
[mutableArray removeAllObjects];

-(void)clickToRegist
{

UIApplication * app = [UIApplication sharedApplication];
   AppDelegate *  user =app.delegate;
[user.window setRootViewController:regist];

}
//设置UIControl的大小
UIControl * control = [[UIControl alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//设置背景颜色
[control setBackgroundColor:[UIColor redColor]];
[control addTarget:self action:@selector(controlClick) forControlEvents:UIControlEventTouchUpInside];
[control addTarget:self action:@selector(touchDwon) forControlEvents:UIControlEventTouchDown];
//把方法移除掉
[control removeTarget:self action:nil forControlEvents:UIControlEventTouchDown];
//添加到视图上
[self.window addSubview:control];
return YES;
}
-(void)touchDwon
{
NSLog(@”没有萌萌哒,只有斯巴达”);
}
-(void)controlClick
{
NSLog(@”今天是8月7日,感觉萌萌哒”);
}
-(void)click
{
NSLog(@”dongcidaci”);
}
@end
//不可变数组
//第一种创建方式
NSArray * array = [NSArray array];
//第二种创建方式 创建的时候 直接添加一个元素
NSArray *array1 = [NSArray arrayWithObject:@”123”];
//第三种 添加多个元素 ,以nil标记结尾
NSArray *array2 = [NSArray arrayWithObjects:@”123”,@”234”,@”345”, nil];
// 第四种 创建方式
NSArray *array4 = [[NSArray alloc] init];
数组不能直接添加数字
People * p = [[People alloc] init];
NSArray *array5 = [NSArray arrayWithObjects:@”123”, p,nil];
//也可以
//解决数字添加到数组中的难题,两种办法
第一种
//第一步,先获取制定索引的字符串
NSString * indexString1 = [array6 objectAtIndex:0];
//第二步,把字符串转换成int
//first
NSArray * array6 = [NSArray arrayWithObjects:@”123”,@”234”,@”345” ,nil];
int number1 = [indexString1 intValue];
NSLog(@”indexString1=====%d”,number1);
//second
int number2 = 100;
NSNumber * num = [NSNumber numberWithInt:number2];
NSString * string2 = [NSString stringWithFormat:@”%d”,number2];
NSArray * array7 = [NSArray arrayWithObjects:string2,nil];

//数组循环

for (int i = 0 ; i < [array6 count]; i++)
{
  //  NSLog(@"---------%@",[array6 objectAtIndex:i]);
    NSLog(@"=========%@",array6[i]);
}
//因为不确定数组里面的元素是什么类型 所以我们用一个id类型
for (id  image in array5)
{
     NSLog(@"array5=========%@",image);
}

//数组 第一,不能直接添加数字
// 第二,数组不能添加空的元素
NSMutableArray * mutableArray = [NSMutableArray arrayWithObjects:@”张三”,@”李四”,@”王五”, nil];

// [mutableArray addObject:nil];

//在指定位置 插入一个元素
[mutableArray insertObject:@"213" atIndex:0];

//删除一个指定索引的元素
[mutableArray removeObjectAtIndex:0];

//替换制定位置的两个元素
[mutableArray replaceObjectAtIndex:0 withObject:@"888888"];

/// AppDelegate.m
LoginViewController * login = [[LoginViewController alloc] init];
//从一个页面跳转到另一个页面,两个页面共用一个window
/// [self.window setRootViewController:login];

LoginViewController.m
RegistViewController *regist = [[RegistViewController alloc] init];
UIApplication * app = [UIApplication sharedApplication];
AppDelegate * user =app.delegate;
[user.window setRootViewController:regist];
//视图将要消失
-(void)viewWillDisappear:(BOOL)animated
//视图将要出现
-(void)viewWillAppear:(BOOL)animated
//视图消失的时候 调用
-(void)viewDidDisappear:(BOOL)animated
//视图出现的时候 调用的方法
-(void)viewDidAppear:(BOOL)animated
//系统接受到内存警告的时候 会自动调用这个方法
- (void)didReceiveMemoryWarning

//使label里面显示label原本的文字与textField里面的文字且不重复显示
要先声明一个全局的allString
allString = label.text;

NSString * textString = textField.text;
label.text = [NSString stringWithFormat:@”%@%@”,allString,textString];
要是重复显示则不用声明全局变量

//第一种方法
//触摸屏幕时,键盘消失
// [self.window endEditing:YES];
//第2⃣️种方法
[textField resignFirstResponder];
//成为第一响应者
// [textField becomeFirstResponder];
//多个button调用同一个方法 需要把多个按钮当作参数传给方法
设置label颜色及透明度
[label setBackgroundColor:[UIColor colorWithRed:100.0/255.0 green:150.0/255.0 blue:145.0/255.0 alpha:0.6]];

//设置动画按钮的实现方法里面 缩放
CGAffineTransform transForm = CGAffineTransformMakeScale(1.5, 1.5);
[UIView beginAnimations:nil context:nil];

跳转页面
[self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
[self.window insertSubview:blueView aboveSubview:redView];
[self.window insertSubview:blueView belowSubview:redView];
[self.window insertSubview:redView atIndex:0];
[self.window bringSubviewToFront:blueView];
[self.window sendSubviewToBack:redView];
自定义动画
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setBackgroundColor:[UIColor redColor]];
[self setText:@”我是动画”];

}
return self;

}
-(void)startAnimation
{
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(animationRun) userInfo:nil repeats:YES];

}
-(void)animationRun
{
CGAffineTransform transForm =CGAffineTransformRotate(self.transform, M_PI_2);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.5];
self.transform = transForm;
[UIView commitAnimations];

}
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
[self startAnimation];

}
//animateWithDuration 动画持续时间
//<#code#> 代码 让你们写代码的实现
// [UIView animateWithDuration:4 animations:^{
// [imageView setFrame:CGRectMake(320-40, 480-40, 40, 40)];
// }];
//
//completion 完成
// [UIView animateWithDuration:3600 animations:^{
// [imageView setFrame:CGRectMake(320-40, 480-40, 40, 40)];
// NSLog(@”瞅啥瞅”);
// // 判断动画是否完成 完成的话调用里面的代码块
// } completion:^(BOOL finished) {
// NSLog(@”瞅你咋了”);
// [imageView setFrame:CGRectMake(arc4random()%280, arc4random()%440, 40, 40)];
// }];
//
// NSLog(@”你在瞅下试试”);
//NSTimer 定时器
/*
scheduled 时间表 计划表
Interval 间隔
scheduledTimerWithTimeInterval 多久调用一次定时器
userInfo 用户信息 传过去的信息
repeats 重复
*/
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];

//static 关键字 静态变量
// 用static关键字声明的变量 只会初始化一次 ,如果被初始化过,就不再初始化
static int i = 1;
//不可变数组
NSArray *tempArray = [NSArray arrayWithObjects:@”小红”,@”小明”,@”小蓝”, nil];
//可变数组
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
[mutableArray addObject:@”1”];
[mutableArray addObject:@”2”];
[mutableArray addObject:@”3”];

[mutableArray removeObjectAtIndex:0];

[mutableArray addObject:@"1000"];
[mutableArray insertObject:@"88" atIndex:0];
[mutableArray replaceObjectAtIndex:0 withObject:@"213"];
[mutableArray removeLastObject];
[mutableArray removeAllObjects];

//把登录视图作为根视图
LoginViewController * login = [[LoginViewController alloc]init];
UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:login];
[self.window setRootViewController:navigationController];
//设置背景色
UIImageView * background = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”dlbg”]];
[self.view addSubview:background];
//设置导航条背景图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@”7”] forBarMetrics:UIBarMetricsDefault];

[self setTitle:@"登录"];

UIBarButtonItem * rightBar = [[UIBarButtonItem alloc]initWithTitle:@”注册” style:UIBarButtonItemStyleDone target:self action:@selector(clickToRegist)];
self.navigationItem.rightBarButtonItem = rightBar;
-(void)clickToRegist
{
RegistViewController * regist = [[RegistViewController alloc]init];

[self.navigationController pushViewController:regist animated:YES];

}
[self.navigationController popToRootViewControllerAnimated:YES];
//给导航条增加背景图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@”1”] forBarMetrics:UIBarMetricsDefault];
//设置返回按钮
//首先 我们如果不设置返回按钮的话 返回按钮的标题默认为这个页面的标题
//设置返回按钮的话 自己添加的返回按钮的方法 是不会起作用的
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithTitle:@”个人中心” style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = back;

@interface AppDelegate : UIResponder

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值