============================== UIView ==========================================
/*******************UIView*********************/
//
UIView * view = [[UIViewalloc] initWithFrame:CGRectMake(100,100, 130,130)];
view.tag =1333;
view.frame =CGRectMake(100,100,300,300);
view.backgroundColor = [UIColorgrayColor];
UIView * view1 = [[UIViewalloc] initWithFrame:CGRectMake(100,100, 200,30)];
view1.backgroundColor = [UIColorblueColor];
//父视图 子视图
[self.windowaddSubview:view1];
[self.windowaddSubview:view];
//隐藏属性
view.hidden =NO;
//透明度
view.alpha =0.1;
////判断tag是否存在
UIView * view2 = (UIView * )[self.windowviewWithTag:1333];
//找到中心点
view1.center = CGPointMake(50+220/2,100+220/2);
//后两个参数是view自己的宽和高,前两个参数影响的是本身view的原点坐标,如果不理解,前两个参数就写0,0
view1.bounds = CGRectMake(0,0, 220,220);
/*******************圆角及阴影**********************/
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,100, 220,220)];
view.backgroundColor = [UIColor redColor];
//设置圆角的角度
view.layer.cornerRadius = 20;
//设置边框的宽度,默认颜色为黑色
view.layer.borderWidth = 4;
//设置边框颜色
view.layer.borderColor = [UIColor greenColor].CGColor;
//YES:不显示边框以外的视图 NO:显示边框以外的视图(都是相对子视图来说的) YES:阴影效果出不来
view.layer.masksToBounds = NO;
//阴影效果
//设置阴影颜色
view.layer.shadowColor = [UIColor grayColor].CGColor;
//设置阴影移动角度
view.layer.shadowOffset = CGSizeMake(100,100);
//设置阴影透明度,默认完全透明
view.layer.shadowOpacity = 1;
//设置阴影虚化角度
view.layer.shadowRadius = 50;
[self.view addSubview:view];
/*******************层次关系***********************/
//移除某个视图
[view4 removeFromSuperview];
//把某个视图移动到最上面
[self.view bringSubviewToFront:view1];
//把某个视图移动到最下面
[self.view sendSubviewToBack:view1];
//将某个视图插入到另一个视图的上面,前面的参数是想要移动到上面的视图
[self.view insertSubview:view2 aboveSubview:view3];
//将某个视图插入到另一个视图的下面,前面的参数是想要移动到下面的视图
[self.view insertSubview:view2 belowSubview:view3];
//将某个视图插入到指定的位置
[self.view insertSubview:view1 atIndex:2];
//交换两个视图的位置
[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:1];
/*******************变形属性************************/
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(150,100, 50,50)];
view.backgroundColor = [UIColor redColor];
//变形属性是以中心点为基准的,view变化,但中心点是不变的
//这个变形属性变化的是本身view的大小,写(1,1)时不变,小于以时以中心点缩小,大于以时以中心点放大
//第一个参数影响width,第二个影响height
view.transform = CGAffineTransformMakeScale(0.5,1);
//角度变形,根据你给的角度进行旋转
view.transform = CGAffineTransformMakeRotation(M_PI_2 / 2);
/*******************页面切换************************/
//去想去的页面,但要包含那页面的头文件,并创建那页面对象
[self presentViewController:view1 animated:YES completion:^{}];
//返回上一页
[self dismissViewControllerAnimated:YES completion:^{}];
//页面切换前后的执行
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"即将出现");
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"已经出现");
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"即将消失");
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"已经消失");
}
================================ UILabel =============================
[UIFontfontWithName:TextFontName_Lightsize:15];
[UIFontfontWithName:TextFontName_Boldsize:16];
[UIFontfontWithName:TextFontNamesize:15];
//创建标签
UILabel * label = [[UILabelalloc]
initWithFrame:CGRectMake(20,30, 270, 30)];
//设置标签内容
label.text =@"国青";
//设置标签颜色
label.textColor = [UIColorblackColor];
//设置标签背景颜色 (clearColor---透明色)
label.backgroundColor = [UIColoryellowColor];
//字体居中
label.textAlignment =NSTextAlignmentCenter;
//字体加粗并设置字号
label.font = [UIFontboldSystemFontOfSize:25];
//字体加粗并设置字号 *** 加粗会被倾斜覆盖
label.font = [UIFontitalicSystemFontOfSize:25];
//遍历字体库
NSArray * array = [UIFontfamilyNames];
for (NSString *strin array) {
NSLog(@"%@",str);
}
//从family找合适的字体
label.font = [UIFontfontWithName:@"Arial"size:25];
//设置文字阴影颜色
label.shadowColor = [UIColororangeColor];
//设置阴影偏移量
label.shadowOffset =CGSizeMake(-5, -5);
//设置为高亮
label.highlighted =YES;
//设置高亮的颜色 当高亮设置颜色时,原有标签颜色被取代
label.highlightedTextColor =[UIColorredColor];
//根据标签大小自动设置字体大小
label.adjustsFontSizeToFitWidth =YES ;
//把标签加到window
[self.windowaddSubview:label];
// UIBaselineAdjustmentAlignBaselines -文字的最上端和label的中线对齐
// UIBaselineAdjustmentAlignCenters -文本的中线和label的中线对齐
// UIBaselineAdjustmentNone -文字的最下端和label的中线对齐
label.baselineAdjustment =UIBaselineAdjustmentAlignCenters;
[self.windowaddSubview:label];
//设置最大显示行数值为零则为不限制最多显示行数
label1.numberOfLines =0;
//设置内容超出宽度的模式(默认以NSLineBreakByWordWrapping)
// NSLineBreakByWordWrapping -以单词为单位换行,以单词为单位截断
// NSLineBreakByCharWrapping -以字符为单位换行,以字符为单位截断
// NSLineBreakByClipping - 以单词为单位换行,以字符为单位截断
// NSLineBreakByTruncatingHead,/* Truncate at head of line: "...wxyz" */
// NSLineBreakByTruncatingTail,/* Truncate at tail of line: "abcd..." */
// NSLineBreakByTruncatingMiddle/* Truncate middle of line: "ab...yz" */
label1.lineBreakMode =NSLineBreakByTruncatingMiddle;
[self.windowaddSubview:label1];
/*******************UIlabel自适应*********************/
CGRect labelRect = [label1.textboundingRectWithSize:CGSizeMake(300,MAXFLOAT)options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label1.font}context:nil];
label1.frame =CGRectMake(10,30, 300,labelRect.size.height);
//设置行间距
NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:comlabel.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc] init];
[paragraphStyle setLineSpacing:30];//调整行间距
[attributedString addAttribute:NSParagraphStyleAttributeNamevalue:paragraphStyle range:NSMakeRange(0, comlabel.text.length)];
comlabel.attributedText = attributedString;
[comlabel sizeToFit];
=============================== UIButton =================================
1、UIButtonTypeSystem= UIButtonTypeRoundedRect直角
2、UIButtonTypeContactAdd蓝色圆圈里面有加号
3、UIButtonTypeDetailDisclosure = UIButtonTypeInfoDark = UIButtonTypeInfoLight 蓝色圆圈里面有叹号
//圆角按钮
UIButton * button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.tag =1;
button.frame =CGRectMake(10 ,30, 300,30);
button.backgroundColor = [UIColorredColor];
//设置按钮标题文字
1、UIControlStateNormal 正常状态
2、UIControlStateHighlighted 高亮状态
3、UIControlStateSelected 选中状态
4、UIControlStateDisabled 禁用状态
[button setTitle:@"圆角按钮" forState:UIControlStateNormal];
[button setTitle:@"圆角按钮被点击" forState:UIControlStateHighlighted];
//设置按钮标题文字颜色
[button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
//设置按钮高亮状态的颜色
[button setTitleColor:[UIColorpurpleColor] forState:UIControlStateHighlighted];
//按钮高亮状态下是否发光
button.showsTouchWhenHighlighted =YES;
//选中状态:默认为正常状态默认为NO
btn.selected = YES;
//禁用状态:默认为YES YES为可用状态,NO为不可用
btn.enabled = YES;
//设置文字的大小
btn.titleLabel.font = [UIFont systemFontOfSize:18.0];
//设置点击事件
/*
Target - 执行某个对象的方法
action - 要执行的方法名
ControlEvents - 触发事件的形式
*/
[button addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
/***************UIButton(图片)********************/
//由图片名字找到图片,并实例化一个UIImage类
UIImage *image = [UIImage imageNamed:@"1.png"];
UIImage *image1 = [UIImage imageNamed:@"map.png"];
//类方法创建图片类型的button **注:后面的类型一定要选UIButtonTypeCustom
UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];
/*
1、读取图片的宽度:image.size.width
2、读取图片的高度:image.size.height
*/
//customBtn.frame = CGRectMake(50, 150, image.size.width, image.size.height);
customBtn.frame = CGRectMake(50,150, 100,50);
customBtn.backgroundColor = [UIColor grayColor];
//设置图片(可有四种状态)
//按钮尺寸小于图片尺寸,图片会被压缩;按钮尺寸大于图片尺寸,图片以原尺寸显示
//[customBtn setImage:image1 forState:UIControlStateNormal];
//图片会随着按钮的尺寸进行拉伸或压缩
[customBtn setBackgroundImage:image1 forState:UIControlStateNormal];
//如果用custom类型,设置既有文字又有图片时,可有两种情况:1、整体按钮大小大于setImage时方法设置的图片的大小时,文字在图片右面显示;整体按钮大小小于 setImage时方法设置的图片的大小时,文字bu显示;2、用setBackgroundImage设置图片时,文字居中显示
[customBtn setTitle:@"button" forState:UIControlStateNormal];
//当图片 自动为蓝色时,用这个
[btn setImage:[[UIImageimageNamed:@"ding2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
============================== UIImageView ==========================================
//1、由名字直接读取图片
UIImage *image = [UIImage imageNamed:@"1.png"];
//实例化
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10,40, image.size.width, image.size.height)];
//相关属性
imageView.image = image;
//2、路径读取
NSString *path = [[NSBundle mainBundle]pathForResource:@"map" ofType:@"png"];
UIImage *image1 = [UIImage imageWithContentsOfFile:path];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10,80, 300,380)];
imageView1.image = image1;
//3、播放动画
NSMutableArray *array = [NSMutableArray array];
for (int i =1; i<=12; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"player%d",i]];
[array addObject:image];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 64,64)];
imageView.backgroundColor = [UIColor grayColor];
//可以打开用户响应的属性;YES:它上面的子视图有响应,NO:它上面的子视图无响应
imageView.userInteractionEnabled = YES;
imageView.tag = 1000;
/*
1、UIViewContentModeScaleToFill 拉伸填满
2、UIViewContentModeScaleAspectFit 按比例填充
3、UIViewContentModeScaleAspectFill 按比例填满
*/
imageView.contentMode = UIViewContentModeScaleToFill;
//设置动画图片(数组)
imageView.animationImages = array;
//设置每次循环的时间(秒)
imageView.animationDuration = 1;
//设置循环次数:0:无限循环 >0的数:循环几次结束
imageView.animationRepeatCount = 0;
[self.view addSubview:imageView];
//开始动画
[imageView startAnimating];
//结束动画
[imageView stopAnimating];
=============================== UITextField =================================
/****************UITextField******************/
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, (self.view.frame.size.height -110), 300,35)];
textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:textField];
//设置边框 (**)
/*
1、UITextBorderStyleNone 无边框
2、UITextBorderStyleLine 直线直角
3、UITextBorderStyleBezel 粗一点
4、UITextBorderStyleRoundedRect 圆角
*/
textField.borderStyle = UITextBorderStyleRoundedRect;
//设置右下角return键的格式 (**)
/*
1、UIReturnKeyDefault return
2、UIReturnKeyJoin Join
3、UIReturnKeyNext Next
*/
te
//设置左视图
textfield.leftViewMode =UITextFieldViewModeAlways;
textfield.leftView = img1View;
//设置左视图
textfield.rightViewMode =UITextFieldViewModeAlways;
textfield.rightView = img1View;
//设置背景图片:只有UITextBorderStyle=UITextBorderStyleNone的时候才有效果
textField.background = [UIImage imageNamed:@"restartServer@2x.png"];
//设置清除按钮(右边的圆圈X号)(**)
textField.clearButtonMode = UITextFieldViewModeAlways;
//是否自动大写 (**)
/*
1、UITextAutocapitalizationTypeNone 不自动大写
2、UITextAutocapitalizationTypeWords 单词的首字母大写
3、UITextAutocapitalizationTypeSentences 句子的首字母大写
4、UITextAutocapitalizationTypeAllCharacters 全部大写
*/
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
//自动纠错 (**)
/*
1、UITextAutocorrectionTypeDefault 默认
2、UITextAutocorrectionTypeNo 不自动纠错
3、UITextAutocorrectionTypeYes 自动纠错
*/
textField.autocorrectionType = UITextAutocorrectionTypeDefault;
//是否可编辑 YES:可以 NO:不可以
textField.enabled = YES;
//提示文字 (**)
textField.placeholder = @"请输入。。。";
//文字颜色
textField.textColor = [UIColor orangeColor];
//提示文字设置颜色
[textField setValue:[UIColorredColor] forKeyPath:@"_placeholderLabel.textColor"];
//提示文字设置字体
[textField setValue:[UIFontboldSystemFontOfSize:16]forKeyPath:@"_placeholderLabel.font"];
//默认文字 (**)
textField.text = @"wenzi";
//字体大小 (**)
//textField.font = [UIFont systemFontOfSize:18.0];
//对齐方式(左右)(*(*)
textField.textAlignment = NSTextAlignmentCenter;
//对齐方式(上下)
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//密文 (**)
textField.secureTextEntry = NO;
//再次编辑清空文字 (*)
textField.clearsOnBeginEditing =YES;
//字体适配输入框大小
textField.adjustsFontSizeToFitWidth = YES;
//设置最小字体 (**)
textField.minimumFontSize = 11.0;
/*!!!!!!!!!!!!!!!代理!!!!!!!!!!!!!!!!!*/ 遵守协议 <UITextFieldDelegate>
textField.delegate = self;
[_searchTextField addTarget:self action:@selector(textFieldChanged) forControlEvents:UIControlEventEditingChanged];
}
#pragma - mark Delegate
//当return键被点击时会触发这个方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//失去第一响应
[textField resignFirstResponder];
//开始第一响应
[textField becomeFirstResponder];
returnYES;
}
//开始编辑时调用
- (void)textFieldDidBeginEditing:(UITextField *)textField
//结束编辑时调用
- (void)textFieldDidEndEditing:(UITextField *)textField
//是否可清除内容
- (BOOL)textFieldShouldClear:(UITextField *)textField
//是否可以开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
//是否可以结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
=============================== UINavigationController =================================
//导航控制器创建前设置导航器的字体大小和颜色
//创建字典
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:30.0],NSForegroundColorAttributeName:[UIColor redColor]};
//整体更改navigationBar上面title的文字大小及颜色
[[UINavigationBar appearance] setTitleTextAttributes:dic];
//导航控制器创建
RootViewController *root = [[RootViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
//btn点击时跳转到下一页
[self.navigationController pushViewController:view2 animated:YES];
//btn点击时跳转到根目录页
[self.navigationController popToRootViewControllerAnimated:YES];
//btn点击时跳转到下一页
[self.navigationController popViewControllerAnimated:YES];
//btn点击时跳转到想去的那页 , 这种方法可以让页面的排序不发生改变
NSArray *array = self.navigationController.viewControllers;
[self.navigationController popToViewController:array[1] animated:YES];
//设置标题头(自定义UIView)
self.navigationItem.titleView = label;
//导航栏的按钮样式1(更改文字)
UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:@selector(leftButton)];
//添加到导航栏的左边
self.navigationItem.leftBarButtonItem = left;
//导航栏的按钮样式2(系统图片)
UIBarButtonItem *right1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(right1Click)];
//添加到导航栏的右边
self.navigationItem.rightBarButtonItem = right1;
//导航栏的按钮样式3(自定义图片)
UIBarButtonItem *right2 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"itemImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(right2Click:)];
//添加到导航栏的右边(一个)
self.navigationItem.rightBarButtonItem = right2;
//右边数组里面有几个,就出现几个 (由右向左)(多个)
//左边的话是由左向右的
self.navigationItem.leftBarButtonItems =@[right1,right2];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(0,0, 32,32);
button1.backgroundColor = [UIColor redColor];
[button1 setTitle:@"right" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
导航栏的按钮样式4(自定义按钮)
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:button1];
self.navigationItem.rightBarButtonItem = rightBtn;
//找到导航栏的方法self.navigationController.navigationBar
//设置导航栏风格:默认UIBarStyleDefault,现在其他三个黑色现在无区别
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
//设置背景颜色:颜色不纯正
self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
//设置颜色:颜色较纯正
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
//竖屏:UIBarMetricsDefault
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar"] forBarMetrics:UIBarMetricsDefault];
//横屏:UIBarMetricsLandscapePhone
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav-32.png"] forBarMetrics:UIBarMetricsLandscapePhone];
//设置标题
self.title =@"页面一";
self.navigationItem.title =@"hhhh";
/************************** UIToolbar下面的工具栏 ******************/
//下面的工具栏(320*44)
self.navigationController.toolbarHidden =NO;
//toolBar.png itemImage.png
//找到ToolBar的方式self.navigationController.toolbar
//更改toolBar的图片
[self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"toolBar.png"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
//设置按钮样式1(系统)
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(itemClick)];
//设置按钮样式2(文字)
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"button" style:UIBarButtonItemStylePlain target:self action:@selector(itemClick)];
//设置按钮样式3(图片)
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"itemImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemClick)];
//创建空格(可均分相应的空位)
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(itemClick)];
//添加到数组里
NSArray *array = @[space,item1,space,item2,space,item3,space];
//设置toolBar的按钮
self.toolbarItems = array;
======================================= 九个小控件 =================================
/************************** 一 UIAlertView 提示信息 ******************/
//创建提示 遵守协议<UIAlertViewDelegate>
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"余额不足" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
alert.tag = 1000;
//展示提示
[alert show];
//提示信息的位置从0开始 从左至右
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
/************************** 二 UIActionSheet 提示信息 ******************/
//创建提示 遵守协议<UIActionSheetDelegate>
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"请选择性别" delegate:self cancelButtonTitle:@"保密" destructiveButtonTitle:nil otherButtonTitles:@"男",@"女",nil];
//展示提示
[actionSheet showInView:self.view];
//提示信息的位置从0开始 从上至下
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
/************************** 三 UIWebView网页视图 ******************/
//以(0,0)为起点,全屏 self.view.bounds 遵守协议<UIWebViewDelegate>
UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
//将字符串转化为网址
NSURL *url = [NSURL URLWithString:@"http://10.0.8.8"];
//请求网址
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
webView.delegate = self;
[self.view addSubview:webView];
//请求网址失败执行 可以打印error
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
//开始请求
- (void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@"start");
}
//结束请求
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"finish");
}
/************************** 四 UISwitch开关控件 *************************************/
//开关控件 frame的后两个参数已固定
UISwitch *mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(10,100, 100,300)];
//开关状态 YES:为开 NO:为关
mySwitch.on = YES;
//响应方法 添加时间
[mySwitch addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mySwitch];
/************************** 五 UIActivityIndicatorView 等待视图控件 ******************/
//等待视图控件 white gray
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
//设置中心点
activity.center = CGPointMake(100,300);
activity.center = self.view.center;
activity.tag = 1000;
[self.view addSubview:activity];
//开始旋转
[activity startAnimating];
//停止旋转
[activity stopAnimating];
/************************** 六 UIStepper 步进器 *************************************/
//步进器
//固定大小(94*29)
UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(20,50, 94,29)];
//最大值
stepper.maximumValue = 100;
//最小值
stepper.minimumValue = 10;
//每步前进或后退多少
stepper.stepValue = 10;
//值是否实时变化
stepper.continuous = NO;
//添加事件
[stepper addTarget:self action:@selector(stepperChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:stepper];
/************************** 七 UIProgressView 进度条 *************************************/
//进度条
//长度可更改,高度固定2
UIProgressView *progress = [[UIProgressView alloc]initWithFrame:CGRectMake(20,100, 280,2)];
progress.progress = 0.1;
progress.backgroundColor = [UIColor redColor];
progress.tag = 1000;
[self.view addSubview:progress];
/************************** 八 UISlider 滑块控件 *************************************/
//滑块控件
//高度固定34
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(20,150, 280,34)];
slider.maximumValue = 20;
slider.minimumValue = 1;
slider.value = 14;
//值是否实时变化
slider.continuous = NO;
[slider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
/************************** 九 UISegmentedControl 分段选择器 *************************************/
//分段选择器
NSArray *array = @[@"免费图片",@"付费",@"赠送"];
UISegmentedControl *sg = [[UISegmentedControl alloc]initWithItems:array];
sg.frame = CGRectMake(20,260, 240,30);
//设置默认选中按钮
sg.selectedSegmentIndex = 1;
//插入某一个按钮
[sg insertSegmentWithTitle:@"火影" atIndex:1 animated:YES];
//sg.segmentedControlStyle = UISegmentedControlStyleBar;
[sg addTarget:self action:@selector(sgChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:sg];
============================== UITabBarViewController 标签栏控制器 ==========================================
FirstViewController *first = [[FirstViewControlleralloc]init];
UINavigationController *nav1 = [[UINavigationControlleralloc] initWithRootViewController:first];
//图片渲染
nav.tabBarItem = [[UITabBarItemalloc] initWithTitle:titles[i]image:[barImages[i] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]tag:0];
//上左下右
[nav.tabBarItemsetImageInsets:UIEdgeInsetsMake(5,0, -5, 0)];
//一张图片(已弃用)(**)
second.tabBarItem.image = [UIImageimageNamed:@"tab_1.png"];
//两张不同的图片(已弃用)(**)
[nav1.tabBarItemsetFinishedSelectedImage:[UIImageimageNamed:@"tab_2.png"]withFinishedUnselectedImage:[UIImageimageNamed:@"tab_3.png"]];
//两张不同的图片(********)
UITabBarItem *item1 = [[UITabBarItemalloc]initWithTitle:@"界面"image:[UIImageimageNamed:@"tab_3.png"]selectedImage:[UIImageimageNamed:@"tab_s.png"]];
//系统的图片(**)
UITabBarItem *item1 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarkstag:0];
first.tabBarItem = item1;
//创建标签栏控制器 遵守协议 <UITabBarControllerDelegate>
UITabBarController *tabBar = [[UITabBarControlleralloc]init];
//tabBar管理一个数组,数组里的元素必须是UIViewController对象(****)
tabBar.viewControllers =@[nav1];
//默认选择第几个页面(****)
tabBar.selectedIndex =3;
//默认选择第几个页面(第二种方式)(****)
tabBar.selectedViewController = viewControllers[1];
//代理(*)
tabBar.delegate =self;
//点击Done调用(可不记)
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
NSLog(@"Done");
}
//点击页面调用(可不记)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(@"%@",viewController.title);
}
//点击Edit调用(可不记)
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers{
NSLog(@"Eidt");
}
============================== UIGestureRecognizer 手势 ==========================================
//所有手势的父类
// UIGestureRecognizer
//****************** 点击手势(****) ***************
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapGesture:)];
//点击次数
tap.numberOfTapsRequired =1;
//需要多少个手指点击
tap.numberOfTouchesRequired =1;
//设置代理
tap.delegate =self;
[imageView addGestureRecognizer:tap];
//点击手势的方法
- (void)tapGesture:(UITapGestureRecognizer *)tap{
NSLog(@"tap");
//手势的view属性,能够找到手势所在的视图view
UIImageView *view = tap.view;
NSLog(@"%ld",(long)view.tag);
}
//****************** 移动手势(****) ***************
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panGesture:)];
pan.delegate =self;
[imageView addGestureRecognizer:pan];
//移动手势的方法
- (void)panGesture:(UIPanGestureRecognizer *)pan{
NSLog(@"pan");
//判断手势目前的状态
if (pan.state ==UIGestureRecognizerStateBegan || pan.state ==UIGestureRecognizerStateChanged) {
//手势在view上的偏移量
CGPoint point = [pantranslationInView:self.view];
//手势的view属性,能够找到手势所在的视图view
UIImageView *imageView = pan.view;
//改变中心点的位置
imageView.center =CGPointMake(imageView.center.x + point.x, imageView.center.y + point.y);
//清除上一次的偏移量,否则手势的偏移量会叠加
//CGPointZero = CGPointMake(0, 0)
[pan setTranslation:CGPointZeroinView:self.view];
}
}
//****************** 缩放手势 ***************
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchGesture:)];
pinch.delegate =self;
[imageView addGestureRecognizer:pinch];
//缩放手势的方法
- (void)pinchGesture:(UIPinchGestureRecognizer *)pinch{
NSLog(@"缩放");
if (pinch.state ==UIGestureRecognizerStateBegan || pinch.state ==UIGestureRecognizerStateChanged) {
//手势的view属性,能够找到手势所在的视图view
UIImageView *imageView = pinch.view;
//第一个参数:图形原本的状态,后两个参数:根据手势的缩小和放大找到系数
imageView.transform =CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
//重置缩放倍数
pinch.scale =1.0;
}
}
//****************** 旋转手势 ***************
UIRotationGestureRecognizer *roration = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rorationGesture:)];
roration.delegate =self;
[imageView addGestureRecognizer:roration];
//旋转手势的方法
- (void)rorationGesture:(UIRotationGestureRecognizer *)rotation{
NSLog(@"旋转");
if (rotation.state ==UIGestureRecognizerStateBegan || rotation.state ==UIGestureRecognizerStateChanged) {
UIImageView *imageView = rotation.view;
//第一个参数:图形原本的状态,第二个参数:手势旋转的角度
imageView.transform =CGAffineTransformRotate(imageView.transform, rotation.rotation);
//重置角度为0
rotation.rotation =0.0;
}
}
//手势的代理
//********************* 设置视图是否可以识别多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([gestureRecognizer.viewisEqual:otherGestureRecognizer.view]) {
returnYES;
}
returnNO;
}
============================== UITouch 触摸view调用的方法 ==========================================
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITextField *textField = (UITextField *)[self.viewviewWithTag:1000];
[textField resignFirstResponder];
NSLog(@"开始触摸");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"移动");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"结束触摸");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"中断触摸");
//电话进来时,调用这个方法
}
//关闭自动布局
self.automaticallyAdjustsScrollViewInsets =NO;
//创建UIImageView
UIImage *image = [UIImageimageNamed:@"3.png"];
UIImageView *imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0,0, image.size.width, image.size.height)];
imageView.image = image;
imageView.tag =1000;
============================== UIScrollView 图片 ==========================================
self.automaticallyAdjustsScrollViewInsets =NO;
//创建UIScrollView 遵守协议<UIScrollViewDelegate>
//全屏的大小
UIScrollView *scrollView = [[UIScrollViewalloc] initWithFrame:self.view.bounds];
//内容的大小
scrollView.contentSize =CGSizeMake(image.size.width, image.size.height);
[self.viewaddSubview:scrollView];
//把imageView添加到ScrollView上
[scrollView addSubview:imageView];
//边界弹的效果,默认yes
scrollView.bounces =NO;
scrollView.backgroundColor = [UIColorredColor];
//关闭水平滑块:默认YES
scrollView.showsHorizontalScrollIndicator =NO;
//关闭垂直滑块:默认YES
scrollView.showsVerticalScrollIndicator =NO;
//内容的偏移量
scrollView.contentOffset =CGPointMake(0,0);
//分页效果:YES带分页效果
scrollView.pagingEnabled =NO;
//设置代理
scrollView.delegate =self;
//缩放倍数
scrollView.maximumZoomScale =2;
scrollView.minimumZoomScale =0.3;
//缩放图片大小的方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
UIImageView *imageView = (UIImageView *)[scrollViewviewWithTag:1000];
return imageView;
}
//结束加速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"结束加速");
}
//scrollView开始滑动就调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"开始滑动");
}
//开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"开始拖动");
}
//结束拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"结束拖动");
}
============================== UIPageControl 图片点点 ==========================================
self.automaticallyAdjustsScrollViewInsets =NO;
//实例化UIScrollView 遵守协议<UIScrollViewDelegate>
//delegate
//隐藏滑块
//关掉ScrollView的弹的效果
//分页
//内容大小 320*9 self.view.frame.size.height
//添加到父视图上
_scrollView = [[UIScrollViewalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height)];
_scrollView.delegate =self;
_scrollView.showsHorizontalScrollIndicator =NO;
_scrollView.showsVerticalScrollIndicator =NO;
_scrollView.bounces =NO;
_scrollView.pagingEnabled =YES;
// _scrollView.contentSize = CGSizeMake(320 * 9, self.view.frame.size.height);
_scrollView.contentSize =CGSizeMake(320,self.view.frame.size.height * 9);
[self.viewaddSubview:_scrollView];
//循环添加图片
for (int i =1; i<10; i++) {
// UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i - 1) * 320, 0, 320, self.view.frame.size.height)];
UIImageView *imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0, (i -1) * self.view.frame.size.height,320, self.view.frame.size.height)];
imageView.image = [UIImageimageNamed:[NSStringstringWithFormat:@"火影0%d.png",i]];
[_scrollViewaddSubview:imageView];
}
//UIPageControl
//实例化
_pageControl = [[UIPageControlalloc]initWithFrame:CGRectMake(10,self.view.frame.size.height - 30, 300, 20)];
_pageControl.center =self.view.center;
//***相关属性
//背景颜色
_pageControl.backgroundColor = [UIColorgrayColor];
//点点的颜色 (不是当前页的点的颜色)
_pageControl.pageIndicatorTintColor = [UIColorwhiteColor];
//点点的颜色 (当前页的点的颜色)
_pageControl.currentPageIndicatorTintColor = [UIColorcyanColor];
//设置页数(点点的个数)
_pageControl.numberOfPages =9;
//设置当前页
_pageControl.currentPage =0;
//旋转(垂直)
_pageControl.transform =CGAffineTransformMakeRotation(M_PI_2);
//添加到父视图
[self.viewaddSubview:_pageControl];
}
//结束加速的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//偏移量
// scrollView.contentOffset
//找到偏移量的位置
CGPoint point = scrollView.contentOffset;
//找到目前是第几页
// int currentPage = point.x / 320;
int currentPage = point.y /self.view.frame.size.height;
//赋给pageControl的currentPage属性
_pageControl.currentPage = currentPage;
}
============================== UITextView 文本输入框可以滑动 ==========================================
//实例化 遵守协议<UITextViewDelegate>
//相关属性
//delegate
//添加到父视图上
UITextView *textView = [[UITextViewalloc] initWithFrame:CGRectMake(20,60, 280,100)];
textView.backgroundColor = [UIColorwhiteColor];
textView.delegate =self;
//相关属性
//没有一键清除、没有边框、没有提示文字属性、没有自动修改、没有密文
//设置文字颜色
textView.textColor = [UIColorblackColor];
//字体大小
textView.font = [UIFontsystemFontOfSize:18.0];
//默认文字
// textView.text = @"yvlhltsaaghjhfdstghhjkkkbggffdswsewfyghjkkkjbvfddgjkljhvcsaaasdfg ,http://www.baidu.com";
//return键的样式
textView.returnKeyType =UIReturnKeyDefault;
//键盘样式
textView.keyboardType =UIKeyboardTypeDefault;
//对齐方式
textView.textAlignment =NSTextAlignmentLeft;
//是否可以拖动 :默认YES
textView.scrollEnabled =YES;
//显示链接模式 只有不可编辑时有效
textView.dataDetectorTypes =UIDataDetectorTypeAll;
//是否可以编辑 :默认YES
textView.editable =YES;
//tag
textView.tag =1000;
//隐藏滑块
textView.showsVerticalScrollIndicator =YES;
//自动大写
textView.autocapitalizationType =UITextAutocapitalizationTypeAllCharacters;
[self.viewaddSubview:textView];
_label = [[UILabelalloc] initWithFrame:CGRectMake(6,0, 280,35)];
_label.backgroundColor = [UIColorclearColor];
_label.text =@"提示文字提示文字";
_label.textColor = [UIColorgrayColor];
_label.textAlignment =NSTextAlignmentLeft;
[textView addSubview:_label];
#pragma mark - Delegate
//是否可以开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
returnYES;
}
//是否可以结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
returnYES;
}
//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView{
// _label.hidden = YES;
}
//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView{
if (textView.text.length ==0) {
_label.hidden =NO;
}
}
//提示文字是出现还是隐藏
- (void)textViewDidChange:(UITextView *)textView{
if (textView.text.length ==0) {
_label.hidden =NO;
}else{
_label.hidden =YES;
}
}
//return键键盘消失
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([textisEqualToString:@"\n"]) {
//键盘消失
[textView resignFirstResponder];
returnNO;
}
returnYES;
}
//
============================================ UITableView 列表 ==========================================
self.automaticallyAdjustsScrollViewInsets =NO;
/*
1、UITableViewStyleGrouped 分组分段
2、UITableViewStylePlain 普通
*/
//实例化
UITableView *tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,64, 320, 480 -64) style:UITableViewStylePlain];
/************重点*************/
//代理协议
tableView.delegate =self;
//数据源协议
tableView.dataSource =self;
//没有横线
_tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
[self.viewaddSubview:tableView];
}
#pragma mark - UITableViewDataSource
//设置每组有多少行的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section ==0) {
return3;
}elseif (section == 1){
return6;
}
return15;
}
//设置每行显示的内容的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *iden = @"iden";
//查询复用池里是否有带iden标识的cell,如果有,直接使用,不再创建
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:iden];
//如果没有
if (cell ==nil) {
//创建标识为iden的cell
/*
1、UITableViewCellStyleDefault 只显示图片和textLabel
2、UITableViewCellStyleSubtitle显示图片、textLabel、detailTextLabel但是textLabel(上面)和detailTextLabel(下 面)是上下显示的
3、UITableViewCellStyleValue1 显示图片、textLabel、detailTextLabel 但是textLabel和detailTextLabel是左右显示的,textLabel左对齐,detailTextLabel右对齐
4、UITableViewCellStyleValue2只显示textLabel和detailTextLabel,textLabel和detailTextLabel左右显示,挨着
*/
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:iden];
}
//cell自己的属性:设置文字
//textLabel属性
cell.textLabel.text = [NSStringstringWithFormat:@"%d",indexPath.row];
//detailTextLabel属性
cell.detailTextLabel.text = [NSStringstringWithFormat:@"detail%d",indexPath.row];
//imageView属性:设置图片
cell.imageView.image = [UIImageimageNamed:@"AppIcon40x40.png"];
//右边的提示图片
/*
1、UITableViewCellAccessoryNone 没有任何图片
2、UITableViewCellAccessoryCheckmark对勾
3、UITableViewCellAccessoryDetailButton 圆圈带!号
4、UITableViewCellAccessoryDetailDisclosureButton圆圈带!号 +右箭头
5、UITableViewCellAccessoryDisclosureIndicator 右箭头
*/
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
//被点击时的状态
/*
1、UITableViewCellSelectionStyleNone 无效果
2、UITableViewCellSelectionStyleBlue点击后灰色
3、UITableViewCellSelectionStyleDefault 点击后灰色
4、UITableViewCellSelectionStyleGray 点击后灰色
*/
cell.selectionStyle =UITableViewCellSelectionStyleNone;
//返回cell
return cell;
}
//返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return3;
}
//添加header的文字(每段的上面)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section ==0) {
return@"这是第一段";
}elseif (section == 1){
return@"这是第二段";
}else{
return@"这是第三段";
}
// return [NSString stringWithFormat:@"%d",section];
}
//添加footer的文字(每段的下面)
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return [NSStringstringWithFormat:@"%d",section];
}
#pragma mark - UITableViewDelegate
//每行的高度(每个cell的高度)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//默认44
// if (indexPath.section == 0) {
// if (indexPath.row == 0) {
// return 80;
// }else{
// return 30;
// }
// }
return50;
}
//设置标题头的高度(header)
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return30;
}
//设置标题尾的高度(footer)
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return30;
}
//点击的方法(*****************)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:[NSStringstringWithFormat:@"您点击了第%d段的第%d行",indexPath.section,indexPath.row] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
//取消选中效果
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
}
//设置标题头view(header)(****)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIViewalloc]init];
view.backgroundColor = [UIColorredColor];
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, 320,30)];
label.text =@"label";
label.backgroundColor = [UIColororangeColor];
label.font = [UIFontsystemFontOfSize:18.0];
label.textColor = [UIColorcyanColor];
label.textAlignment =NSTextAlignmentCenter;
[view addSubview:label];
return view;
}
//************************* 删除、插入、移动 *********************************
//设置标题尾view(footer)
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
returnnil;
//设置方式和设置标题头的方式一样
}
//编辑状态的设置
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//
if (indexPath.section ==0) {
//设置为删除
returnUITableViewCellEditingStyleDelete;
}
//设置为插入
returnUITableViewCellEditingStyleInsert;
}
//编辑事件的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//删除事件
if (editingStyle ==UITableViewCellEditingStyleDelete) {
//删除对应的数据源
[[_dataArrobjectAtIndex:indexPath.section]removeObjectAtIndex:indexPath.row];
//删除对应的cell
//[NSArray arrayWithObject:indexPath] 找到对应的cell
[tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}
//插入事件
if (editingStyle ==UITableViewCellEditingStyleInsert) {
Person *person = [[Personalloc]init];
person.name =@"我是新来的";
//更改对应数据源
[[_dataArrobjectAtIndex:indexPath.section]insertObject:person atIndex:indexPath.row];
//插入cell
//[NSArray arrayWithObject:indexPath] 找到对应的cell
[tableView insertRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
//删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return@"删掉你";
}
#pragma mark - 移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//找到要移动的对象
Person *person = [[_dataArrobjectAtIndex:sourceIndexPath.section]objectAtIndex:sourceIndexPath.row];
//从原来位置删除
[[_dataArrobjectAtIndex:sourceIndexPath.section]removeObjectAtIndex:sourceIndexPath.row];
//加到新位置
[[_dataArrobjectAtIndex:destinationIndexPath.section]insertObject:person atIndex:destinationIndexPath.row];
}
//编辑按钮
- (void)editClick{
NSLog(@"editClick");
//tableView的编辑属性 _tableView.editing 默认NO
//更改编辑状态
[_tableViewsetEditing:!_tableView.editinganimated:YES];
// NO -> YES -> NO
}
//刷新按钮
- (void)refreshClick{
NSLog(@"refreshClick");
//刷新数据
//执行这个方法,会把代理里的方法重新执行一遍
[_tableViewreloadData];
}
//************************* 选中多行删除 *********************************
//专有按钮 (简写)
self.navigationItem.leftBarButtonItem =self.editButtonItem;
//右边删除按钮
_deleteItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrashtarget:selfaction:@selector(deleteClick)];
#pragma mark - self.editButtonItem的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[supersetEditing:editing animated:animated];
//更改tableView的编辑状态
[_tableViewsetEditing:editing animated:YES];
// [_tableView setEditing:!_tableView.editing animated:YES];
//清除删除的数组
[_tempArrremoveAllObjects];
if (editing) {
[self.navigationItemsetRightBarButtonItem:_deleteItemanimated:YES];
}else{
[self.navigationItemsetRightBarButtonItem:nilanimated:YES];
}
}
//多选按钮出现
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//删除状态和插入状态一起返回,会出现多选按钮
returnUITableViewCellEditingStyleDelete |UITableViewCellEditingStyleInsert;
}
//cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.editing ==YES) {
//将选中的那个对象放到删除数组里
[_tempArraddObject:[_dataArrobjectAtIndex:indexPath.row]];
}else{
//当tableView不可编辑时,做其他操作
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
}
}
//反选事件
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.editing ==YES) {
//反选操作,将对应的对象从删除数组里面删除掉
[_tempArrremoveObject:[_dataArrobjectAtIndex:indexPath.row]];
}
}
//删除按钮
- (void)deleteClick{
//将一个数组删除另一个数组
[_dataArrremoveObjectsInArray:_tempArr];
//刷新数据
[_tableViewreloadData];
}
//************************* 索引 *********************************
//索引条的背景色,文字颜色,点击时的颜色
_tableView.sectionIndexColor = [UIColorredColor];
_tableView.sectionIndexBackgroundColor = [UIColorgrayColor];
_tableView.sectionIndexTrackingBackgroundColor = [UIColorcyanColor];
}
//设置索引
#pragma mark - 索引设置
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray *arr = [[NSMutableArrayalloc]initWithCapacity:0];
[arr addObject:[NSStringstringWithFormat:@"#"]];
for (int i ='a'; i <= 'z'; i++) {
[arr addObject:[NSStringstringWithFormat:@"%c",i]];
}
return arr;
}
//索引点击事件,index对应返回tableView的第几段
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
if (index ==0) {
NSLog(@"uui");
}
return index -1;
}
//************************* 折叠 *********************************
//存放BOOL值,用来检测我们的折叠状态
[_closeArraddObject:[NSNumbernumberWithBool:YES]];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//如果某个分段的closeArr为yes,折叠状态,返回0
if ([[_closeArrobjectAtIndex:section] boolValue]) {
return0;
}
return [[_dataArrobjectAtIndex:section] count];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//做标题头,标题头为UIButton
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame =CGRectMake(0,0, 320,35);
button.backgroundColor = [UIColorredColor];
button.tag = section +1000;
[button setTitle:[NSStringstringWithFormat:@"%c",'a' + section]forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (void)buttonClick:(UIButton *)button{
NSLog(@"buttonClick");
//找到对应的折叠状态
BOOL close = [[_closeArrobjectAtIndex:button.tag -1000] boolValue];
//修改zhedie状态
[_closeArrreplaceObjectAtIndex:button.tag -1000 withObject:[NSNumbernumberWithBool:!close]];
//重载tableView NSIndexSet:理解成存放数字的集合
[_tableViewreloadSections:[NSIndexSetindexSetWithIndex:button.tag -1000] withRowAnimation:UITableViewRowAnimationAutomatic];
}
//************************* 搜索 *********************************
//创建搜索条
_searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,0, 320,44)];
//整个tableView的标题头
_tableView.tableHeaderView =_searchBar;
//创建搜索控制器 : 让我们的搜索条和页面、控制器相关联
_sdc = [[UISearchDisplayControlleralloc]initWithSearchBar:_searchBarcontentsController:self];
//UISearchDisplayController管理了一个tableView
//它管理的tableView也需要实现代理
_sdc.searchResultsDataSource =self;
_sdc.searchResultsDelegate =self;
//设置UISearchDisplayController自己的代理
_sdc.delegate =self;
}
#pragma mark - UITableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//如果响应这个方法的tableView不是我们创建的tableView,那么就是搜索控制器创建的tableView
if (tableView !=_tableView) {
return1;
}
return_dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView !=_tableView){
return_resultArr.count;
}
return [[_dataArrobjectAtIndex:section] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (tableView !=_tableView) {
return@"搜索结果";
}
return [NSStringstringWithFormat:@"%c",'a'+section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *iden = @"iden";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:iden];
if (cell ==nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:iden];
}
if (tableView !=_tableView) {
cell.textLabel.text = [_resultArrobjectAtIndex:indexPath.row];
}else{
cell.textLabel.text = [[_dataArrobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
}
return cell;
}
//当搜索文字发生变化就会调用这个方法
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
//清空上一次搜索的对象
[_resultArrremoveAllObjects];
//遍历数组
for (NSArray *arrin _dataArr) {
for (NSString *strin arr) {
NSRange range = [strrangeOfString:searchString];
if (range.length) {
//如果有就添加到结果里
[_resultArraddObject:str];
}
}
}
returnYES;
}
//************************* 搜索 *********************************
//************************* 折叠 *********************************
//************************* 索引 *********************************
#import "RootViewController.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate>
@property (nonatomic ,strong) UITableView * tableView;
@property (nonatomic ,strong) NSMutableArray * dataArr;
//存储搜索栏
@property (nonatomic ,strong) NSMutableArray * tempArr;
//装是否折叠状态
@property (nonatomic ,strong) NSMutableArray * closeArr;
//搜索栏
@property (nonatomic ,strong) UISearchBar * sb;
@property (nonatomic ,strong) UISearchDisplayController *sdc;
@end
@implementation RootViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.automaticallyAdjustsScrollViewInsets =NO;
//************* 数据源 ********
_dataArr = [[NSMutableArrayalloc] init];
_tempArr = [[NSMutableArrayalloc] init];
_closeArr = [[NSMutableArrayalloc] init];
for (int i ='a'; i <= 'z' ; i++) {
NSMutableArray * arr = [[NSMutableArrayalloc] init];
for (int j =0; j < 20; j++) {
[arr addObject:[NSStringstringWithFormat:@"%c%d",i,j]];
}
[_dataArraddObject:arr];
//存放BOOL值,用来检测我们的折叠状态
[_closeArraddObject:[NSNumbernumberWithBool:NO]];
}
//************** tableview实例化 ********
_tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0,20, 320, 460)style:UITableViewStylePlain];
_tableView.delegate =self;
_tableView.dataSource =self;
[self.viewaddSubview:_tableView];
//************** 搜索栏实例化 *********
_sb = [[UISearchBaralloc] initWithFrame:CGRectMake(0,20, 320, 44)];
_tableView.tableHeaderView =_sb;
_sdc = [[UISearchDisplayControlleralloc] initWithSearchBar:_sbcontentsController:self];
_sdc.searchResultsDelegate =self;
_sdc.searchResultsDataSource =self;
_sdc.delegate =self;
//************* 索引设置 *************
_tableView.sectionIndexColor = [UIColorgrayColor];
_tableView.sectionIndexBackgroundColor = [UIColorgreenColor];
_tableView.sectionIndexTrackingBackgroundColor = [UIColorredColor];
}
//************ tableView ****************
//多少栏
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView !=_tableView) {
return1;
}
return_dataArr.count;
}
//多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView !=_tableView) {
return_tempArr.count;
}
else if ([[_closeArrobjectAtIndex:section] boolValue]) {
return0;
}
else{
return [_dataArr[section]count];
}
}
//cell设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * iden = @"iden";
UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:iden];
if (cell ==nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:iden];
}
if (tableView !=_tableView) {
cell.textLabel.text =_tempArr[indexPath.row];
}
else{
cell.textLabel.text = [_dataArr[indexPath.section]objectAtIndex:indexPath.row];
}
return cell;
}
//设置分栏标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSStringstringWithFormat:@"%c",'a'+section];
}
//************** 搜索方法 ********************
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[_tempArrremoveAllObjects];
for (NSArray * arrin _dataArr) {
for (NSString * strin arr) {
NSRange range = [strrangeOfString:searchString];
if (range.length) {
[_tempArraddObject:str];
}
}
}
[_tableViewreloadData];
returnYES;
}
//************* 索引设置 *********************
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray * arr =[[NSMutableArrayalloc] init];
for (int i ='a' ; i <= 'z' ; i++) {
[arr addObject:[NSStringstringWithFormat:@"%c",i]];
}
return arr;
}
//************* 折叠方法 *********************
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton * btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
btn.frame =CGRectMake(0,0, 320,60);
[btn setTitle:[NSStringstringWithFormat:@"%c",'a'+section]forState:UIControlStateNormal];
btn.tag =1000+section;
btn.backgroundColor = [UIColoryellowColor];
[btn addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
return btn;
}
-(void)btnClick:(UIButton *)btn
{
BOOL close = [_closeArr[btn.tag -1000]boolValue];
[_closeArrreplaceObjectAtIndex:btn.tag-1000withObject:[NSNumbernumberWithBool:!close]];
//重载tableView NSIndexSet:理解成存放数字的集合
[_tableViewreloadSections:[NSIndexSetindexSetWithIndex:btn.tag -1000] withRowAnimation:UITableViewRowAnimationAutomatic];
}
//************************* 瀑布流 *********************************
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//设置相同的偏移量
if (scrollView ==_aTableView) {
_bTableView.contentOffset =_aTableView.contentOffset;
}else{
_aTableView.contentOffset =_bTableView.contentOffset;
}
}
//************************* UICollectionView *********************************
#import "RootViewController.h"
#import "MyCollectionViewCell.h"
@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) NSArray *dataArray;
@end
@implementation RootViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorwhiteColor];
_dataArray =@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15"];
//UICollectionView的自动布局
//自动布局的类 UICollectionViewFlowLayout->UICollectionViewLayout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc]init];
//设置滑动方向
//UICollectionViewScrollDirectionHorizontal 水平滑动
//UICollectionViewScrollDirectionVertical 竖直滑动
layout.scrollDirection =UICollectionViewScrollDirectionHorizontal;
//设置水平间距
// layout.minimumInteritemSpacing = 30;
//设置竖直间距
// layout.minimumLineSpacing = 30;
//创建UICollectionView
UICollectionView *collectionView = [[UICollectionViewalloc]initWithFrame:CGRectMake(0,20, 320, 460)collectionViewLayout:layout];
collectionView.backgroundColor = [UIColorgrayColor];
//代理
collectionView.dataSource =self;
collectionView.delegate =self;
[self.viewaddSubview:collectionView];
//注册cell类
[collectionView registerClass:[MyCollectionViewCellclass] forCellWithReuseIdentifier:@"iden"];
}
#pragma mark - dataSouce
//设置段数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return1;
}
//设置单元格个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return_dataArray.count;
}
//设置单元格的内容
//UICollectionViewCell只能用自定义的cell,只能注册后再用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"iden"forIndexPath:indexPath];
cell.nameLabel.text =_dataArray[indexPath.row];
cell.imageView.image = [UIImageimageNamed:[NSStringstringWithFormat:@"10_%d.jpg",indexPath.row]];
return cell;
}
#pragma mark - delegate
//设置item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//第一个参数是宽,第二个参数是高
//竖直滑动时:
//item排列:最左边的item根据collectionView的最左边的点去做长和高(向左向下),最右边的item根据collectionView的最右边的点去做长和高(向右向下)
//水平滑动时:
//item排列:最上边和左边的item根据collectionView的最上边和左边的点去做长和高(向左向下),最下边和最左边的item根据collectionView的最下边和最左边的点去做长和高(向右向上)
returnCGSizeMake(110,130);
}
//collectionView边边间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//上左下右
returnUIEdgeInsetsMake(5,5, 5,5);
}
//设置水平间隙
//默认10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return50;
}
//设置竖直间隙
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return100;
}
//选中某个item的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%d",indexPath.row);
}
//************************* UICollectionView 标题头标题尾 *********************************
#import "MyCollectionReusableView.h"
@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//注册cell类
[_collectionViewregisterClass:[MyCollectionViewCellclass] forCellWithReuseIdentifier:@"iden"];
//注册标题头类
//UICollectionElementKindSectionHeader
[_collectionViewregisterClass:[MyCollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"view"];
//注册标题脚类
//UICollectionElementKindSectionFooter
[_collectionViewregisterClass:[MyCollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"view"];
//标题头的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//第二个参数竖直滑动时有效,第一个参数水平滑动时有效
returnCGSizeMake(30,30);
}
//标题脚的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
//第二个参数竖直滑动时有效,第一个参数水平滑动时有效
returnCGSizeMake(30,30);
}
//设置自定义标题头和脚
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
MyCollectionReusableView *view = [collectionViewdequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"view"forIndexPath:indexPath];
if (kind ==UICollectionElementKindSectionHeader) {
view.label.text = [NSStringstringWithFormat:@"标题头%d",indexPath.section];
}else{
view.label.text = [NSStringstringWithFormat:@"标题脚%d",indexPath.section];
}
return view;
}
//************************* NSNotificationCenter 通知中心 *********************************
//通知(注册通知)
[[NSNotificationCenterdefaultCenter] postNotificationName:@"post"object:nil];
//注销
- (void)dealloc
{
[[NSNotificationCenterdefaultCenter] removeObserver:selfname:@"post"object:nil];
}
- (void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
//使用通知
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(reloaded)name:@"post"object:nil];
}
- (void)reloaded{
//更新tableView的数据
[_tableViewreloadData];
}