一、OC基本语法
//整型
NSInteger a = 10;
//浮点型
CGFloat b = 2.3;
//布尔类型
BOOL flag = YES;
//字符串NSString(只要是对象类型,占位符全是%@)
//length字符串的长度
NSLog(@"str的长度= %ld",str.length);
//字符串相等(全等,前缀,后缀)
//1全等isEqualToString:
if ([str isEqualToString:@"abcde"]) {
NSLog(@"是的");
}
//2前缀相等hasPrefix
if([str hasPrefix:@"a"]){
NSLog(@"前缀等于a");
}
//3后缀相等hasSuffix
if([str hasSuffix:@"de"]){
NSLog(@"后缀等于de");
}
//格式化创建字符串
NSString *str1 = [NSString stringWithFormat:@"%@++++++",str];
二、OC基础语法
//数组(NSArray/NSMutableArray(不可变数组/可变数组))
// 1不可变数组NSArray
NSArray *array1 = @[@"a",@"b",@"c",@"d"];
//数组元素个数.count
NSLog(@"count = %ld",array1.count);
//通过下标访问数组里面的元素
NSString *str = array1[0];
// 2可变数组NSMutableArray
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil];
//元素个数:count
//添加元素addObject
[mutableArray addObject:@"5"];
//移除元素removeObject
[mutableArray removeObject:@"3"];
//字典(存放多个键值对的一种数据类型)(NSDictionary不可变,NSMutableDictionary可变)
//不可变字典
NSDictionary *dict = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3"};
//所有的key值,所有的value值
NSLog(@"allkeys = %@,allvalue = %@",dict.allKeys,dict.allValues);
三、 oc方法格式
//调用方法用空格,方法调用结束用中括号表示
[self func1];
NSInteger num = [self func2];
NSInteger length = [self lengOfString:@"12345"];
//OC方法的格式:
// +表示类方法,只能用类来调用,
// -表示实例方法,用对象调用
//无参数输入的方法格式:+/- (方法的返回值)方法名
-(void)func1{
NSLog(@"%s",__func__);
}
-(NSInteger)func2{
NSLog(@"%s",__func__);
return 20;
}
//有参数输入的方法格式:+/-(方法的返回值)方法名:(参数1类型)参数1名:(参数2类型)参数2名
//内存溢出的时候调用
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
四、 UI常用控件
//创建数组
self.imageNames = @[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];
//frame表明了控件的坐标和宽高(CGRect类型)
//按钮控件UIButton
//设置按钮坐标高度
self.leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 150, 45, 45)];
//关闭交互
self.leftBtn.userInteractionEnabled = NO;
//根据名字显示图片
UIImage *leftImage = [UIImage imageNamed:@"left_disable"];
//给按钮指定背景图片
[self.leftBtn setBackgroundImage:leftImage forState:(UIControlStateNormal)];
//添加到界面
[self.view addSubview:self.leftBtn];
//监听按钮
[self.leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
//设置相框坐标高度
self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(85 , 100, 200, 200)];
//设置显示的图片
UIImage *image = [UIImage imageNamed:@"biaoqingdi"];
self.myImageView.image = image;
//添加到界面
[self.view addSubview:self.myImageView];
//获取当前是第几张图片
NSInteger index = [self.imageNames indexOfObject:self.titleLable.text];
//交互开启
self.leftBtn.userInteractionEnabled = YES;
//说明下一个标签
NSString *nextTitle = self.imageNames[index+1];
//获取标签名称
self.titleLable.text = nextTitle;
//切换到下一张图片
self.myImageView.image = [UIImage imageNamed:nextTitle];
//获取当前是第几张图片
NSInteger index = [self.imageNames indexOfObject:self.titleLable.text];
//相框UIImageView
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(150, 70, 200, 200)];
//设置imageView显示的图片
imageView.image = image1;
[self.view addSubview:imageView];
//标签控件UILable
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(150, 30, 150, 30)];
//设置标签的文本
lable.text = @"表情弟";
//设置居中方式
lable.textAlignment = NSTextAlignmentCenter;
//设置标签颜色
lable.textColor = [UIColor redColor];
五、汤姆猫
//新建类的方法名
UIImageView *imageView = [UIImageView new];
//序列帧动画要播放的图片数组
imageView.animationImages
//动画时长
imageView.animationDuration
//动画重复次数
imageView.animationRepeatCount
//开始动画
[imageView startAnimating];
//结束动画
[imageView stopAnimating];
//是否正在执行动画
[imageView isAnimating];
//汤姆猫根据名字和数量执行动画
[selftomCatAnimationWithName:@"eat"WithCount:40];
//创建可变数组images,负责存放要播放的图片数组
NSMutableArray *images = [NSMutableArray array];
//根据i来加载图片,然后添加到可变数组images里面
NSString *imageName = [NSString stringWithFormat:@"eat_%02ld.jpg",i];
//根据格式化的图片名加载图片image
UIImage *image = [UIImage imageNamed:imageName];
//将图片image添加到数组images中
[images addObject:image];
//创建可变数组images,负责存放要播放的图片数组
NSMutableArray*images = [NSMutableArrayarray];
//借助i循环创建图片
for(NSIntegeri =0;i < count;i++) {}
//将name拼接
NSString*imageName = [NSStringstringWithFormat:@"%@_%02ld.jpg",name,i];
//根据格式化的图片名加载图片image
UIImage*image = [UIImageimageNamed:imageName];
//将图片image添加到数组images中
[imagesaddObject:image];
六、访问系统相册
//创建初始化
//所以的能看到的UI控件创建初始化方式都可以采用alloc initWithFrame
self.userBtn= [[UIButtonalloc]initWithFrame:CGRectMake(175,60,80,80)];
//设置圆形半径
self.userBtn.layer.cornerRadius=40;
//圆形超过方框的部分切割掉
self.userBtn.layer.masksToBounds=YES;
//添加点击事件:去访问系统相册
[self.userBtnaddTarget:selfaction:@selector(setUserImage)forControlEvents:(UIControlEventTouchUpInside)];
//添加到屏幕上面来
[self.viewaddSubview:self.userBtn];
//创建系统相册
UIImagePickerController*imagePicker = [[UIImagePickerControlleralloc]init];
//设置代理,到@interface后面遵守协议,拷贝
imagePicker.delegate=self;
//弹出系统相册
[selfpresentViewController:(imagePicker)animated:YEScompletion:
nil];
//这个方法是协议UIImagePickerControllerDelegate里面的,选择图片结束的时候就会自动调用
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(nullableNSDictionary *)editingInfo
{
//设置头像
[self.userBtnsetBackgroundImage:imageforState:(UIControlStateNormal)];
//将系统相册消失掉
[pickerdismissViewControllerAnimated:YEScompletion:nil];
}
七、轮播图
//宏定义
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
//滑动视图UIScrollView,自带了可滑动功能
//创建初始化滑动视图
self.scrollView= [[UIScrollViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
//根据i循环创建UIImageView,再添加到滑动视图UIScrollView上面
UIImageView*imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(i*screenWidth,0,screenWidth,screenHeight)];
NSString*imagename =nil;
//加载响应的图片
UIImage*image = [UIImageimageNamed:imagename];
//设置图片
imageView.image= image ;
//将imageview添加到滑动视图上面
[self.scrollViewaddSubview:imageView];
//设置滑动视图的滑动区域contentSize
self.scrollView.contentSize=CGSizeMake(6*screenWidth,screenHeight);
//整屏翻转
self.scrollView.pagingEnabled=YES;
//边界回弹
self.scrollView.bounces=NO;
//设置代理,代理是负责监听滑动是要整个过程的
self.scrollView.delegate=self;
//添加到滑动视图屏幕上面
[self.viewaddSubview:self.scrollView];
//contentoffset是访问到了滑动视图的偏移量,包含了x,和y轴的偏移量
//setContentOffset:animated:
NSLog(@"offset.x = %f,offset.y = %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
八、弹框
//弹框:UIAlertController(Title:大标题 message:小标题 preferredStyle:弹框样式)
UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"你有病"message:@"你可以去吃药了"preferredStyle:UIAlertControllerStyleAlert];
//给弹框添加行为
[alertControlleraddAction:okAction];
//给弹框取消行为
[alertControlleraddAction:cancelAction];
九、界面
//设置文本字段坐标宽高
self.TextField1= [[UITextFieldalloc]initWithFrame:CGRectMake(100,250,250,25)];
//设置文本字段提示文字
self.TextField1.placeholder=@"请输入账号";
//开启密文
self.TextField2.secureTextEntry=YES;
//设置键盘类型(默认键盘)
self.TextField1.keyboardType=UIKeyboardTypeDefault;
//设置键盘类型(数字键盘)
self.TextField2.keyboardType=UIKeyboardTypeNumberPad;
//将文本字段添加到屏幕上
[self.viewaddSubview:self.TextField2];
网友评论