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

你可能感兴趣的:(iOS培训总结)