iOS培训总结

一:OC基础语法1

//整型

NSInteger a =10;

//NSLog是OC里面的打印函数

NSLog(@"a = %ld",a);

//浮点型

CGFloat b =2.3;

NSLog(@"b = %.2f",b);

//布尔类型

BOOL flag =YES;

//字符串NSString(只要是对象类型,占符全部都是%@)

NSString *str =@"abcde";

NSLog(@"str = %@",str);

//length字符串的长度

NSLog(@"str的长度= %ld",str.length);

//字符串相等(全等,前缀,后缀)

//1,全等

if ([strisEqualToString:@"abcde"]){

NSLog(@"是的");

}

//2,前缀相等

if ([strhasPrefix:@"a"]){

NSLog(@"前缀等于a");

}

//3,后缀相等

if ([strhasSuffix:@"de"]){

NSLog(@"后缀等于de");

}

//格式化创建字符串

NSString *str1 = [NSString stringWithFormat:@"im"];

NSLog(@"str = %@",str1);

二:OC基础语法2

//数组(NSArry/NSMutableArry)

//不可变数组

NSArray *array1 =@[@"a",@"b",@"c",@"d"];

NSLog(@"array1 = %@",array1);

//数组元素个数.count

NSLog(@"count = %ld",array1.count);

//通过下标访问数组里面的元素

NSString *str = array1[0];

NSLog(@"str = %@",str);

//可变数组NSMutableArray

NSMutableArray *mutableArray = [NSMutableArrayarrayWithObjects:@"1",@"2",@"3",@"4",nil];

NSLog(@"mutableArray = %@",mutableArray);

//添加

[mutableArrayaddObject:@"5"];

NSLog(@"已添加-----%@",mutableArray);

//移除

[mutableArrayremoveObject:@"3"];

NSLog(@"已移除-----%@",mutableArray);

//字典(存放)(NSDictionary,NSMutableDictionary)

//不可变字典NSDictionary

NSDictionary *dict =@{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3"};

NSLog(@"dict = %@",dict);

NSString *string =[dictobjectForKey:@"key1"];

NSLog(@"string = %@",string);

//所有的key值,所有的value值

NSLog(@"allValue = %@,allValue = %@",dict.allKeys,dict.allValues);

三:OC方法格式

//调用方法用空格,方法调用结束用括号表示

[self func1];

NSInteger num = [self func2];

NSLog(@"num = %ld",num);

NSInteger length = [selflengOfstring:@"12345"];

NSLog(@"length = %ld",length);

//+表示类方法,只能用类来调用,-表示实例方法,用对象调用

//无参数输入的方法格式:+/-(方法的返回值)方法名

- (void)func1

{

NSLog(@"%s",__func__);

}

-(NSInteger)func2{

NSLog(@"%s",__func__);

return20;

}

//有参数输入的方法格式:=+/-(方法的返回值)方法名:(参数1类型)参数1名方法名:(参数2类型)参数2名

//输入字符串,返回字符串长度

-(NSInteger)lengOfstring:(NSString*)string

{

returnstring.length;

}

//内存溢出的时候调用

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

}

四:UI常用控件

//按钮UIButton (UIButton *button =[UIButton buttonWithType:UIButtonTypeInfoDark];

)

//frame表明了控件的坐标和宽高(CGRect类型)

  UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(20(x),50(y),80(宽),80(高))];

[button setTitle:@"火影" forState:UIControlStateNormal];

//根据名字加载图片

  UIImage *image = [UIImageimageNamed:@"left_normal"];

//给按钮设置背景图片(forState表明按钮状态)

  [buttonsetBackgroundImage:ImageforState:(UIControlStateNormal)];

//给按钮设置背景图片颜色

  button.backgroundColor = [UIColor redColor];

//按钮的监听

[buttonaddTarget:selfaction:@selector(btnClickLister)forControlEvents:UIControlEventTouchUpInside];

//添加按钮到视图上面

[self.viewaddSubview:button];

//相框UIImageView

UIImageView *imageview = [[UIImageViewalloc]initWithFrame:CGRectMake(150(x),50(y),200(宽),200(高))];

UIImage *image1 = [UIImageimageNamed:@"biaoqingdi"];

//设置imageView显示的图片

imageview.image= image1;

[self.viewaddSubview:imageview];

//标签UILabel

UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(150(x),270(y),150(宽),30(高))];

//设置标签文本

label.text=@"XUANG";

//设置居中方式

label.textAlignment=NSTextAlignmentCenter;

//设置标签文本颜色

label.textColor= [UIColorredColor];

//添加标签到视图上面

[self.viewaddSubview:label];}

例:(实例应用):显示图片

   #import"ViewController.h"

@interfaceViewController()

//标题标签

@property(nonatomic,strong)UILabel*titleLabel;

//左边按钮

@property(nonatomic,strong)UIButton*leftBtn;

//右边按钮

@property(nonatomic,strong)UIButton*rightBtn;

//显示图片

@property(nonatomic,strong)UIImageView*myImageView;

//定义数组名

@property(nonatomic,strong)NSArray*imageNames;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.imageNames=@[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];

//定义标签位置与名称

self.titleLabel= [[UILabelalloc]initWithFrame:CGRectMake(150,50,150,30)];

self.titleLabel.text=@"biaoqingdi";

[self.viewaddSubview:self.titleLabel];

//定义做按钮的位置

self.leftBtn= [[UIButtonalloc]initWithFrame:CGRectMake(20,150,45,45)];

//定义按钮的图片

UIImage *leftImage = [UIImageimageNamed:@"left_disablez"];

//设置左按钮的背景图片

[self.leftBtnsetBackgroundImage:leftImageforState:(UIControlStateNormal)];

[self.viewaddSubview:self.leftBtn];

//显示相框名称

self.myImageView= [[UIImageViewalloc]initWithFrame:CGRectMake(85,100,200,200)];

UIImage *image = [UIImageimageNamed:@"biaoqingdi"];

self.myImageView.image= image;

//显示相框图片

[self.viewaddSubview:self.myImageView];

//设置右按钮的位置

self.rightBtn=[[UIButtonalloc]initWithFrame:CGRectMake(305,150,45,45)];

//设置右按钮的图片

UIImage *rightImage = [UIImageimageNamed:@"right_normal"];

//设置右按钮的背景图片

[self.rightBtnsetBackgroundImage:rightImageforState:(UIControlStateNormal)];

[self.viewaddSubview:self.rightBtn];

//按钮的监听

[self.rightBtnaddTarget:selfaction:@selector(rightBtnAction)forControlEvents:(UIControlEventTouchUpInside)];

[self.leftBtnaddTarget:selfaction:@selector(leftBtnAction)forControlEvents:(UIControlEventTouchUpInside)];

}

-(void)rightBtnAction

{

//切换到下一张图片

//获取当前是第几张图片

NSInteger index = [self.imageNamesindexOfObject:self.titleLabel.text];

//不是为最后一张才切换到下一张

if(index <4){

NSString *nextTitle =self.imageNames[index+1];

self.titleLabel.text= nextTitle;

self.myImageView.image= [UIImageimageNamed:nextTitle];

}

}

-(void)leftBtnAction

{

NSInteger index = [self.imageNamesindexOfObject:self.titleLabel.text];

if(index >=0){

NSString *nextTitle =self.imageNames[index-1];

self.titleLabel.text= nextTitle;

self.myImageView.image= [UIImageimageNamed:nextTitle];

}

}

-(void)btnClickLister

{

NSLog(@"click btn");

}

五:序列帧动画

//序列帧动画要播放的图片数组

// imageView.animationImages

//动画时长

// imageView.animationDuration

//动画重复次数

// imageView。animationRepeatCount

//开始动画

// [imageView startAnimating]

//结束动画

// [imageView stopAnimating]

//是否执行动画

// [imageView isAnimating]

//序列帧动画要播放的图片数组

// imageView.animationImages

//动画时长

// imageView.animationDuration

//动画重复次数

// imageView。animationRepeatCount

//开始动画

// [imageView startAnimating]

//结束动画

// [imageView stopAnimating]

//是否执行动画

// [imageView isAnimating]

例:汤姆猫动画(1)

// // ViewController.m //汤姆猫 // // Created by lanou on 16/7/12. //Copyright © 2016年lanou. All rights reserved. // #import"ViewController.h" @interfaceViewController() @property(weak,nonatomic)IBOutletUIImageView*tomcatview; @end @implementationViewController - (void)viewDidLoad { [superviewDidLoad]; } - (IBAction)eatBirdAction:(UIButton*)sender { //创建可变数组 NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<40;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"eat_%02ld.jpg",i]; //根据格式化的图片名加载图片 UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } //设置动画图片数组 self.tomcatview.animationImages= images; //设置动画时长 self.tomcatview.animationDuration=40*0.075; //设置动画重复次数 self.tomcatview.animationRepeatCount=1; //开始动画 [self.tomcatviewstartAnimating]; } (IBAction)drink:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<81;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"drink_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=81*0.075; self.tomcatview.animationRepeatCount=1; [self.tomcatviewstartAnimating]; } - (IBAction)cymbal:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<13;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"cymbal_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=13*0.075; self.tomcatview.animationRepeatCount=1; [self.tomcatviewstartAnimating]; } - (IBAction)fart:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<28;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"fart_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=28*0.075; self.tomcatview.animationRepeatCount=1; [self.tomcatviewstartAnimating]; } - (IBAction)pie:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<24;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"pie_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=24*0.075; self.tomcatview.animationRepeatCount=1; self.tomcatviewstartAnimating]; } - (IBAction)scratch:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<56;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"scratch_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=56*0.075; self.tomcatview.animationRepeatCount=1; self.tomcatviewstartAnimating]; } - (IBAction)footleft:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<30;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"footleft_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=30*0.075; self.tomcatview.animationRepeatCount=1; [self.tomcatviewstartAnimating]; } - (IBAction)footright:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<30;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"footright_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=30*0.075; self.tomcatview.animationRepeatCount=1; [self.tomcatviewstartAnimating]; } - (IBAction)stomach:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<34;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"stomach_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=34*0.075; self.tomcatview.animationRepeatCount=1; [self.tomcatviewstartAnimating]; } - (IBAction)angry:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<26;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"angry_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=26*0.075; self.tomcatview.animationRepeatCount=1; [self.tomcatviewstartAnimating]; } - (IBAction)knockout:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<81;i++) { //根据i来加载图片,然后添加到可变数组image里面 NSString*imageName = [NSStringstringWithFormat:@"knockout_%02ld.jpg",i]; UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } self.tomcatview.animationImages= images; self.tomcatview.animationDuration=81*0.075; self.tomcatview.animationRepeatCount=1; [self.tomcatviewstartAnimating]; } -(void)didReceiveMemoryWarning { [superdidReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

例:汤姆猫动画(2)

// // ViewController.m // 汤姆猫 // // Created by lanou on 16/7/12. // Copyright ?0?8 2016年 lanou. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *tomcatview; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)eatBirdAction:(UIButton *)sender { [self tomCatAnimationWithName:@"eat" withCount:40];(调用编好的代码组,方便快捷) } - (IBAction)drink:(UIButton *)sender { [self tomCatAnimationWithName:@"drink" withCount:81];(调用编好的代码组,方便快捷) } - (IBAction)cymbal:(UIButton *)sender { [self tomCatAnimationWithName:@"cymbal" withCount:13];(调用编好的代码组,方便快捷) } - (IBAction)fart:(UIButton *)sender { [self tomCatAnimationWithName:@"fart" withCount:28];(调用编好的代码组,方便快捷) } - (IBAction)pie:(UIButton *)sender { [self tomCatAnimationWithName:@"pie" withCount:24];(调用编好的代码组,方便快捷) } - (IBAction)scratch:(UIButton *)sender { [self tomCatAnimationWithName:@"scratch" withCount:56];(调用编好的代码组,方便快捷) } - (IBAction)footleft:(UIButton *)sender { [self tomCatAnimationWithName:@"footleft" withCount:30];(调用编好的代码组,方便快捷) } - (IBAction)footright:(UIButton *)sender { [self tomCatAnimationWithName:@"footright" withCount:30];(调用编好的代码组,方便快捷) } - (IBAction)stomach:(UIButton *)sender { [self tomCatAnimationWithName:@"stomach" withCount:34];(调用编好的代码组,方便快捷) } - (IBAction)angry:(UIButton *)sender { [self tomCatAnimationWithName:@"angry" withCount:26];(调用编好的代码组,方便快捷) } - (IBAction)knockout:(UIButton *)sender { [self tomCatAnimationWithName:@"knockout" withCount:81];(调用编好的代码组,方便快捷) } //代码组 -(void)tomCatAnimationWithName:(NSString*)name withCount:(NSInteger)count { if([self.tomcatview isAnimating]){ return; } //创建可变数组 NSMutableArray *images = [NSMutableArray array]; for (NSInteger i = 0;i< count;i++) { //    根据i来加载图片,然后添加到可变数组image里面 NSString *imageName = [NSString stringWithFormat:@"%@_%02ld.jpg",name,i]; //   根据格式化的图片名加载图片 UIImage *image = [UIImage imageNamed:imageName]; [images addObject:image]; } //  设置动画图片数组 self.tomcatview.animationImages = images; // 设置动画时长 self.tomcatview.animationDuration = count*0.075; //  设置动画重复次数 self.tomcatview.animationRepeatCount = 1; //  开始动画 [self.tomcatview startAnimating]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; //Dspose of any resources that can be recreated. } @end

六:访问系统相册

//ViewController.m //访问系统相册 // //Created by lanou on 16/7/12. //Copyright?0?8 2016年lanou.All rights reserved. // #import"ViewController.h" //遵守协议 @interface ViewController() @property(nonatomic,strong)UIButton*userBtn; @end @implementation ViewController -(void)viewDidLoad{ [superviewDidLoad]; //所有能看到的UI控件创建初始化方式都可以采用alloc initWithFrame self.userBtn=[[UIButtonalloc]initWithFrame:CGRectMake(20,60,80,80)]; //设置颜色 self.userBtn.backgroundColor=[UIColorredColor]; //设置圆形半径 self.userBtn.layer.cornerRadius=40; self.userBtn.layer.masksToBounds=YES; //添加点击事件:去访问系统相册 [self.userBtnaddTarget:selfaction:@selector(setUserImage)forControlEvents:(UIControlEventTouchUpInside)]; //添加按钮到屏幕上来 [self.viewaddSubview:self.userBtn]; } //访问系统相册 -(void)setUserImage { //创建系统相册 UIImagePickerController*imagePicker=[[UIImagePickerControlleralloc]init]; //设置代理,到@interface后面遵守协议 imagePicker.delegate=self; //弹出系统相册 [selfpresentViewController:imagePickeranimated:YEScompletion:nil]; } //这个方法是协议UIImagePickerControllerDelegate里面的,选择图片结束的时候就会自动调用 -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(nullableNSDictionary

*)editingInfo { //设置头像 [self.userBtnsetBackgroundImage:imageforState:(UIControlStateNormal)]; //将系统相册消失 [pickerdismissViewControllerAnimated:YEScompletion:nil]; } -(void)didReceiveMemoryWarning{ [superdidReceiveMemoryWarning]; //Dispose of any resources that can be recreated. } @end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容