一:OC基礎語法1
//整型
NSInteger a =10;
//NSLog是OC里面的打印函數(shù)
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");
}
//格式化創(chuàng)建字符串
NSString *str1 = [NSString stringWithFormat:@"im"];
NSLog(@"str = %@",str1);
二:OC基礎語法2
//數(shù)組(NSArry/NSMutableArry)
//不可變數(shù)組
NSArray *array1 =@[@"a",@"b",@"c",@"d"];
NSLog(@"array1 = %@",array1);
//數(shù)組元素個數(shù).count
NSLog(@"count = %ld",array1.count);
//通過下標訪問數(shù)組里面的元素
NSString *str = array1[0];
NSLog(@"str = %@",str);
//可變數(shù)組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);
//+表示類方法,只能用類來調用当凡,-表示實例方法山害,用對象調用
//無參數(shù)輸入的方法格式:+/-(方法的返回值)方法名
- (void)func1
{
NSLog(@"%s",__func__);
}
-(NSInteger)func2{
NSLog(@"%s",__func__);
return20;
}
//有參數(shù)輸入的方法格式:=+/-(方法的返回值)方法名:(參數(shù)1類型)參數(shù)1名方法名:(參數(shù)2類型)參數(shù)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表明按鈕狀態(tài))
? [buttonsetBackgroundImage:ImageforState:(UIControlStateNormal)];
//給按鈕設置背景圖片顏色
? button.backgroundColor = [UIColor redColor];
//按鈕的監(jiān)聽
[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;
//定義數(shù)組名
@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];
//按鈕的監(jiān)聽
[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");
}
五:序列幀動畫
//序列幀動畫要播放的圖片數(shù)組
// imageView.animationImages
//動畫時長
// imageView.animationDuration
//動畫重復次數(shù)
// imageView。animationRepeatCount
//開始動畫
// [imageView startAnimating]
//結束動畫
// [imageView stopAnimating]
//是否執(zhí)行動畫
// [imageView isAnimating]
//序列幀動畫要播放的圖片數(shù)組
// imageView.animationImages
//動畫時長
// imageView.animationDuration
//動畫重復次數(shù)
// imageView浪慌。animationRepeatCount
//開始動畫
// [imageView startAnimating]
//結束動畫
// [imageView stopAnimating]
//是否執(zhí)行動畫
// [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 { //創(chuàng)建可變數(shù)組 NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<40;i++) { //根據i來加載圖片冤荆,然后添加到可變數(shù)組image里面 NSString*imageName = [NSStringstringWithFormat:@"eat_%02ld.jpg",i]; //根據格式化的圖片名加載圖片 UIImage*image = [UIImageimageNamed:imageName]; [imagesaddObject:image]; } //設置動畫圖片數(shù)組 self.tomcatview.animationImages= images; //設置動畫時長 self.tomcatview.animationDuration=40*0.075; //設置動畫重復次數(shù) self.tomcatview.animationRepeatCount=1; //開始動畫 [self.tomcatviewstartAnimating]; } (IBAction)drink:(UIButton*)sender { NSMutableArray*images = [NSMutableArrayarray]; for(NSIntegeri =0;i<81;i++) { //根據i來加載圖片,然后添加到可變數(shù)組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來加載圖片权纤,然后添加到可變數(shù)組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來加載圖片钓简,然后添加到可變數(shù)組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來加載圖片,然后添加到可變數(shù)組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來加載圖片汹想,然后添加到可變數(shù)組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來加載圖片外邓,然后添加到可變數(shù)組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來加載圖片,然后添加到可變數(shù)組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來加載圖片古掏,然后添加到可變數(shù)組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來加載圖片损话,然后添加到可變數(shù)組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來加載圖片,然后添加到可變數(shù)組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; } //創(chuàng)建可變數(shù)組 NSMutableArray *images = [NSMutableArray array]; for (NSInteger i = 0;i< count;i++) { // ? ?根據i來加載圖片,然后添加到可變數(shù)組image里面 NSString *imageName = [NSString stringWithFormat:@"%@_%02ld.jpg",name,i]; // ? 根據格式化的圖片名加載圖片 UIImage *image = [UIImage imageNamed:imageName]; [images addObject:image]; } // ?設置動畫圖片數(shù)組 self.tomcatview.animationImages = images; // 設置動畫時長 self.tomcatview.animationDuration = count*0.075; // ?設置動畫重復次數(shù) self.tomcatview.animationRepeatCount = 1; // ?開始動畫 [self.tomcatview startAnimating]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; //Dspose of any resources that can be recreated. } @end
六:訪問系統(tǒng)相冊
//ViewController.m //訪問系統(tǒng)相冊 // //Created by lanou on 16/7/12. //Copyright?0?8 2016年lanou.All rights reserved. // #import"ViewController.h" //遵守協(xié)議 @interface ViewController() @property(nonatomic,strong)UIButton*userBtn; @end @implementation ViewController -(void)viewDidLoad{ [superviewDidLoad]; //所有能看到的UI控件創(chuàng)建初始化方式都可以采用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; //添加點擊事件:去訪問系統(tǒng)相冊 [self.userBtnaddTarget:selfaction:@selector(setUserImage)forControlEvents:(UIControlEventTouchUpInside)]; //添加按鈕到屏幕上來 [self.viewaddSubview:self.userBtn]; } //訪問系統(tǒng)相冊 -(void)setUserImage { //創(chuàng)建系統(tǒng)相冊 UIImagePickerController*imagePicker=[[UIImagePickerControlleralloc]init]; //設置代理践付,到@interface后面遵守協(xié)議 imagePicker.delegate=self; //彈出系統(tǒng)相冊 [selfpresentViewController:imagePickeranimated:YEScompletion:nil]; } //這個方法是協(xié)議UIImagePickerControllerDelegate里面的秦士,選擇圖片結束的時候就會自動調用 -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(nullableNSDictionary
*)editingInfo { //設置頭像 [self.userBtnsetBackgroundImage:imageforState:(UIControlStateNormal)]; //將系統(tǒng)相冊消失 [pickerdismissViewControllerAnimated:YEScompletion:nil]; } -(void)didReceiveMemoryWarning{ [superdidReceiveMemoryWarning]; //Dispose of any resources that can be recreated. } @end