/*
* 注:
* 有任何错误或者不全的地方望谅解;
* 多数是自己的代码,不喜勿喷;
* 很多地方可以把括号收起来,直接看步骤;
*/
"程序的运的几个阶段
第一个运行的,即第一步: 初始化方法,适合进行数据的初始化
- (id)init{}
第二步: load加载,view视图控制器自带的那个view
- (void)loadView{}
第三步: 视图控制器自带的view被加载完成了
- (void)viewDidLoad {}
第四步: 视图控制器默认的view将要显示在屏幕上
- (void)viewWillAppear:(BOOL)animated{}
第五步: 视图控制器的默认view已经显示在屏幕上了
- (void)viewDidAppear:(BOOL)animated{}
第六步: self.view将要消失
- (void)viewWillDisappear:(BOOL)animated{}
第七步: self.view消失了
- (void)viewDidDisappear:(BOOL)animated{}
下面列举一些在开发中可能用得上的UI控件("红色表明最常用",'蓝色代表一般',黑色代表几乎不用)
" UITableView 表格 UIButton: 按钮
" UIAlertView: 警告框 UILabel: 文本标签
" UIImageView 图片显示 UITextField: 文本输入框
" UIScrollView 滚动的控件 UIToolbar: 工具条
" UICollectionView 九宫格 UINavigationBar: 导航条
' UISwitch 开关 UIPickerView: 选择器
' UIActivityIndicator 圈圈 UIDatePicker: 日期选择器
' UIActionSheet 底部弹框 UIPageControl: 分页控件
' UIWebView 网页显示控件 UITextView: 能滚动的文字显示控件
' UIStepper 步进控件
UIProgressView 进度条 UISlider 滑块
UISegmentControl 选项卡
" 使用方法都不经相同,
①' 创建一个对应控件或者视图的实例;
②' 添加到父视图(将这个设置为第二步,以免设置属性后忘记添加);
③' 设置相关属性(大小,颜色,标题,样式,等等);
④' 设置相关事件或者特殊属性
" UIview: 父视图
几个基本"方法
- (void)addSubview:(UIView *)view; //添加一个子控件view
- (void)removeFromSuperview; //从父控件中移除
- (UIView *)viewWithTag:(NSInteger)tag; //根据一个tag标识找出对应的控件(一般都是子控件)
/*常用控件*/
" UIButton: 按钮
" 使用方法:
①,创建一个对应控件或者视图:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
②'添加到父视图:
[self.view addSubview:btn];
③'设置相关属性: (显示文字,文字字体,文字颜色,背景图片,内部缩小图片)
{
button.frame = CGRectMake(100, 100, 100, 100);
[button setBackgroundColor:[UIColor redColor]];
[button setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal]; //普通状态
[button setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted]; //选中状态
button.titleLabel.text = @"这是一个按钮"; //按钮上的文字
}
④'设置相关事件或者特殊属性:
4.1 按钮的点击事件
[button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
4.2 设置按钮圆角
nameView.layer.cornerRadius = 5;
nameView.layer.masksToBounds = YES;
4.3 按钮的四个状态
{
UIControlStateNormalnormal; //正常状态
UIControlStateNormalhighlighted; //高亮状态
UIControlStateNormaldisabled; //不可选
UIControlStateNormalselected; //被选后状态
}
" UILabel: 文本标签
" 使用方法:
①' 创建一个对应控件或者视图的实例;
UILabel *label = [[UILabel alloc]init];
②' 添加到父视图(将这个设置为第二步,以免设置属性后忘记添加);
[self.view addSubview:label];
③' 设置相关属性(大小,颜色,标题,样式,等等);
{
label.text = @"这是个label”;
label.frame = CGRectMake(50, 50, 200, 150);
label.backgroundColor = [UIColor lightGrayColor];
label.font = [UIFont systemFontOfSize:30];
label.textColor = [UIColor colorWithRed:100/255.0 green:20/255.0 blue:189/255.0 alpha:1]; //alpha :透明度
}
④' 设置相关事件或者特殊属性
label.numberOfLines = 0; //文字换行显示
' self.label.textAlignment = NSTextAlignmentCenter; '//常见的文字的对其方式:Center:居中 ; Left: 左对齐 Right: 又对齐
" UITextField 文本输入框
" 使用方法:
①' 创建一个对应控件或者视图的实例(略);
②' 添加到父视图(略);
③' 设置相关属性(略);
④' 设置相关事件或者特殊属性
textField.secureTextEntry = YES; 设置输入的文本为保密类型,(*号类型);
textField.placeholder = @"这是文本框的内容"; //文本框的提示文字
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 触碰到控制器的视图时,自动调用此方法
[self.view endEditing:YES]; // 关闭键盘时机一:点击空白处时关键盘
}
// 关闭键盘时机二:点击键盘右下角按键
// 选中文本框,连线,选择Event为Did End On Exit
- (IBAction)closeKeyboard:(id)sender{
[textField becomeFirstResponder]; //文本框成为第一响应者
[textField resignFirstResponder]; //文本框放弃第一响应者
}
" UITableView 表格
" 使用方法:
①' 创建一个对应控件或者视图的实例(略);
②' 添加到父视图(略);
③' 设置相关属性(略);
④' 设置相关事件或者特殊属性
" UIImageView 图片视图
" 使用方法:
①' 创建一个对应控件或者视图的实例(略);
②' 添加到父视图(略);
③' 设置相关属性:
imageView.frame = CGRectMake(30, 30, 100, 300); //设置大小;
imageView.image = [UIImage imageNamed:@“picture00.png”]; //设置要显示的图片
④' 设置相关事件或者特殊属性
// 触摸控制器的视图,就执行该方法,点击图片,切换到另外一张图片;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
self.index++;
// 格式化字符串,生成图片的名称
NSString *fileName = [NSString stringWithFormat:@"fbb0%ld.jpg",self.index];
// 根据图片名,设置新的图片
self.imageView.image = [UIImage imageNamed:fileName];
}
" UIAlertView: 警告框
" 作 用: 在屏幕中间弹出,起到提示性或警醒性的提醒作用的视图
" 特殊属性: UIAlertAction(警告框中的按钮); alert.textFields(警告框中的文本框)
" 使用方法:
"step1": 创建UIAlertController的实例,必须设置第三个参数为UIAlertControllerStyleAlert
' UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"一段消息message" preferredStyle:UIAlertControllerStyleAlert];
"step2":创建UIAlertAction的实例,可以将UIAlertAction看成警告框中的按钮,既包含按钮上的文字,也包含点击此按钮后要做的响应
' UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) '{
UITextField *nameTextField = alert.textFields[0]; //获取用户在文本框中输入的用户名和密码
UITextField *pwdTextField = alert.textFields[1];
}];
"step3":将Action的实例添加到AlertController中
[alert addAction:action1];
"step4":为AlertController添加文本框
' [alert addTextFieldWithConfigurationHandler:^(UITextField *textField)' {
textField.textColor = [UIColor redColor];
textField.placeholder =@"用户名";
}];
"step5":从当前控制器,推出显示"AlertControllerController
[self presentViewController:alert animated:YES completion:nil]; //UIActionSheet的推出方式与此相同,
" UIActionSheet 底部弹框
" 作 用: 在屏幕底部弹出,起到提示性或警醒性的提醒作用的视图
" 使用方法:
"step1": 创建UIAlertController的实例,必须设置第三个参数为UIAlertControllerStyleActionSheet
' UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleActionSheet];
"step2": 创建UIAlertAction实例,并添加到AlertController中
' UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"普通" style:UIAlertActionStyleDefault handler:nil];
[actionSheet addAction:action1];
"step3": 从当前控制器推出显示AlertController即可
[self presentViewController:actionSheet animated:YES completion:nil];
"注 意": 在UIActionSheet中创建按钮实例的时候用"UIAlertAction"初始化,分为三种样式:{
UIAlertActionStyleDefault //普通按钮
UIAlertActionStyleCancel //取消按钮,默认在最下面,
UIAlertActionStyleDestructive //破坏性按钮,默认字体颜色为红色,起到警告作用;
}
" UINavigationController 导航控制器
" 创建:
代码方式:
//首先创建一个导航控制器,它不能独立存在,必须带有一个副视图或者控制器;
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:avc]; //此处avc是一个视图的实例
self.window.rootViewController = navi;
视图方式: 略
" UINavigationBar 导航条
//配置导航栏
-(void)setupNavigationBar
{
// 1.配置导航栏的右侧按钮
// 文字类型的按钮
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"增加" style:UIBarButtonItemStylePlain target:self action:@selector(add:)];
// 系统类型的按钮
UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(clickCamera:)];
self.navigationItem.rightBarButtonItems = @[item1,item2];
// 2.配置导航栏的左侧按钮
UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wifi_mid"] style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.leftBarButtonItem = item3;
// 3.配置导航栏的中间部分
self.navigationItem.title = @"标题";
UIButton *titleButton = [[UIButton alloc]init];
titleButton.frame = CGRectMake(0, 0, 200, 40);
titleButton.backgroundColor = [UIColor redColor];
[titleButton setTitle:@"张三" forState:UIControlStateNormal];
[titleButton setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
// 只有通过设置按钮的selected属性为YES
// 按钮才会进入selected状态
[titleButton setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateSelected];
[titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 设置按钮中图片的内边距,实现左右位移
titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, 90, 0, 0);
titleButton.titleEdgeInsets;
titleButton.contentEdgeInsets;
// 为按钮添加点击事件
[titleButton addTarget:self action:@selector(clickTitleButton:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;
}
" UIScrollView 滚动的控件
" 使用方法:
/*普通控件:*/
" UIStepper 步进控件
作用:记录一个浮点数,提供两个按钮,一个+号,一个 -号,点击按钮后,修改背后记录的这个数 核心属性:"value
self.stepper.minimumValue = 12;//最小
self.stepper.maximumValue = 100;//最大
self.stepper.value = 30;//当前值
self.stepper.stepValue = 1;//步数
" UISlider 滑块控件
作 用: 记录一个浮点数,提供一个圆球按钮,通过拖拽滑动的方式,快速的,在某个范围内得到一个可变化;
但是不能精确控制的一个数字 属性与事件:"value
属 性: minimumValue; maximumValue;
minimumValueImage; maximumValueImage;
@property(nonatomic,getter=isContinuous) BOOL continuous; //是否连续
" UISwitch 开关控件
作 用:记录一个布尔值,提供了一个按钮,通过点击,则实现这个布尔值在YES和NO之间的变换
属性与事件:on (BOOL类型)
self.label.text = self.mySwitch.on?@"打开":@"关闭";
[self.Switch setOn:!self.Switch.on animated:YES];
" UISegmentedControl 分段控件
作 用: 分段选择;
特殊属性: selectedSegmentIndex; //被选中时的Index值
使用方法:
1. '建立一个数组,用于储存所有图片;
2. '初始化数组,并将图片赋值进去;
3. '设置按钮监听事件;
4. 根据"SegmentedControl"控件的特殊属性"selectedSegmentIndex"设置对应图片数组下标的图片;
" UIActivitylndicatoc 指示器:
" UIProgressView 进度条
" UIDatePicker
" UIPikerView
获得Plist文件的全路径
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"images" ofType:@"plist"];
加载plist文件
_images = [NSArray arrayWithContentsOfFile:path];
- (NSArray *)images{
if (_images == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"];
_images = [NSArray arrayWithContentsOfFile:path];
}
return _images;
}
" 设置代理的三步曲:
第一步:定义协议:
1.引用要定义的委托方类名即当前类名; '例':{
' @class InputViewController;
}
2.协议名称:当前类名+Delegate; '例':{
' @protocol InputViewControllerDelegate <NSObject>
}
2.方法:
2.1:第一个参数一定是委托方自己
2.2:方法名要尽量体现发消息的时机
2.3:待返回的数据是以参数的形式存在 '例':{
-(void)inputViewController:(InputViewController *)vc didFinishInput:(NSString *)msg;
}
第二步:增加id类型的属性记录代理对象 '例':{
@property(nonatomic,weak)id<InputViewControllerDelegate> delegate;
}
第三步:在返回之前,给代理发消息 '例':{
- (IBAction)clickBackButton:(id)sender
{ //在返回之前调用代理,(即给代理发消息);
[self.delegate inputViewController:self didFinishInput:self.textField.text];
[self.navigationController popViewControllerAnimated:YES];
}
}
" 从一个界面推出另外一个界面:
一:'没有导航的情况下'推出一个界面用"present", 返回推出的那个界面用"dismiss" '例':{
[self presentViewController:[[TRSecondViewController alloc]init] animated:YES completion:nil];
[self dismissViewController:[[TRSecondViewController alloc]init] animated:YES completion:nil];
}
二:'有导航的情况下'推出一个界面用"push", 返回用"pop" '例':{
[self pushViewController:[[TRSecondViewController alloc]init] animated:YES completion:nil];
[self popViewController:[[TRSecondViewController alloc]init] animated:YES completion:nil];
}
/*
故事板中所有场景的控制器的实例,都是由故事板在需要显示时
自动创建的,但在自动创建实例时,故事板既不会调用init方法
也不会调用initWithNibName方法,而是自动调用initWithCoder方法
此方法绝对不要自己调用,没有故事板时,此方法无用
*/
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
//配置tabBarItem上面的选中时的图片
self.navigationController.tabBarItem.selectedImage = [UIImage imageNamed:@"full_bell"];
}
return self;
}
/*
日期: 2015.11.24
*/
" 1.苹果移动设备分辨率即坐标:
4/4s 640x960 320x480@2x
5/5c/5s 640x1136 320x658@2x
6/6s 750x1334 375x667@2x
6 Plus/6s Plus 1242x2208 414x736@3x
" 2.利用切面技术,设置滑块的背景美化:
setMinimumTrackImage//设置最小值方向的背景图
setMaximumTrackImage//设置最大值方向的背景图
setThumbImage//设置中间按钮的图片
'例': {
[self.slider setMinimumTrackImage:[UIImage imageNamed:@"playing_volumn_slide_bg"] forState:UIControlStateNormal];
[self.slider setMaximumTrackImage:[UIImage imageNamed:@"playing_volumn_slide_foreground"] forState:UIControlStateNormal];
[self.slider setThumbImage:[UIImage imageNamed:@"playing_volumn_slide_sound_icon"] forState:UIControlStateNormal];
}
" 3.利用代码设置9切片技术:
UIImage *newImage =[image resizableImageWithCapInsets:UIEdgeInsetsMake(10, 12, 10, 12) resizingMode:UIImageResizingModeStretch];
" 4.颜色风格设置:
3.1' self.window.tintColor影响整个应用的风格,除非某一个视图特别设置了自己的tintColor颜色;
3.2' 当出现多个场景的时候,可以设置"GlobalTintColor"设置全局控件的颜色风格;
" 5.UIAppearance: "/*appearance: |əˈpɪərəns|,出现,到来,外表,此处个人理解为全局设置
*/
使用方法: '遵守此协议的对象,可以批量设置某种控件的外观(颜色、贴图等)'例:{
[ [ UISlider appearance] setTintColor: [ UIColor redColor ] ];
//对UIButton类型,字体颜色进行统一的设置
[[UIButton appearance] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//统一设置按钮背景图
[[UIButton appearance] setBackgroundImage:[UIImage imageNamed:@"ad02"] forState:UIControlStateNormal];
}
' 5.1导航栏美化': 利用"appearance"进行设置导航栏的各种属性; "attribute":属性的意思;
包括:'背景色,状态栏颜色,按钮的样式文字颜色等,'例:{
//美化导航栏
//设置背景色
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
//设置有导航栏时,状态栏的文字颜色为白色
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
//设置左右按钮上的文字颜色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
//设置中间的title的文字样式
[[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:
[UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor yellowColor]}];
//设置返回按钮中出现的箭头样式
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"tabbar_item_my_music"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"tabbar_item_my_music"]];
//设置导航条的背景图
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_back"]
forBarMetrics:UIBarMetricsDefault];
}
' 5.2工具栏美化': 同上利用"appearance"设置工具栏的各种属性;
包括:'背景图片,被选中时的图片与颜色,题目的文字,大小,颜色';例:{
//设置整个tabbar的背景图
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbar_back"]];
//设置tabbar上每一个项被选中时的背景图
[[UITabBar appearance]setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected_back"]];
//设置tabbaritem上 题目的位置
[[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -4)];
//设置tabbaritem上的题目的文字大小 和 颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14],
NSForegroundColorAttributeName: [UIColor lightGrayColor]} forState:UIControlStateNormal];
//设置被选中状态颜色和大小
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14],
NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateSelected];
}
"注意": 导航栏和工具栏的设置方式不尽相同;在设置"TitleText"的属性时候,用到"setTitleTextAttributes:@{...}forState:..."的时候,text的多种属性之间用逗号隔开;
/*
2015.11.25
*/
" 如何实现绘制?
Core Graphics
" 贝塞尔曲线的基本方法:
1. 创建一个类,继承自UIView
2. 实现 drawRect方法,在此方法中编写绘制的代码; '例':{
- (void)drawRect:(CGRect)rect{};
}
3. 绘制图形的基本套路:
3.1 获取"bezierPath"对象实例; '例':{
UIBezierPath *path = [UIBezierPath bezierPath]; //获取UIBezierPath的实例;
}
3.2 描绘图形; '例':{
[path moveToPoint:CGPointMake(40, 40)];
[path addLineToPoint:CGPointMake(40, 140)];
[path addLineToPoint:CGPointMake(140, 40)];
}
3.3 设置相关属性; '例':{
// 设置线宽
path.lineWidth = 20;
// 设置线头的样式
path.lineCapStyle = kCGLineCapRound;
// 设置线的连接处的样式
path.lineJoinStyle = kCGLineJoinBevel;
// 3. 设置描边或填充的颜色
[[UIColor redColor] setStroke];
[[UIColor greenColor] setFill];
}
4. 实现绘制; 'Example:' {
// 5. 真的绘制(可以描边 可以填充)
[path stroke];
[path fill];
}
" 日期格式化成字符串":{
NSDate *date = self.datePicker.date;
NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
dateformat.dateFormat = @"yyyy-mm-dd HH-mm-ss";
NSString *datestring = [dateformat stringFromDate:date];
self.label.text = datestring;
}
/*
2015.11.26
*/
/*
2015.11.27
*/
" 生词:
Rotation :旋转;
Pinch :缩放;
Pan :表示移动;
Gesture :表示动作;
Recognize:被意识到;
Location :位置,坐标
Scale :等级
" 手势
手势的总类: 旋转,缩放,移动
1.如何使用手势对象:
1.1: 创建手势对象
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchView:)];
UISwipeGestureRecognizer * swipeGR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(touchValue:)];
1.2: 在创建的同时,指定手势真的发生时,给哪个对象发送消息;
1.3: 可以对手势的常规设置;
tapGR.numberOfTapsRequired = 1;//设置点击次数
tapGR.numberOfTouchesRequired = 1; //设置触控个数
1.4: 将手势绑定到指定的视图上;
[self.view addGestureRecognizer:tapGR];
CGPoint point = [tap locationInView:self.view]; //获取触摸点
" 设置移动的时候用到的方法: CGAffineTransfromTranslate(self.transfrom,x,y);
//用于实现两个手势的同时使用,但必须遵守协议: UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
简易画板制作:
1.定义全局变量Path 并懒加载;
2.在touch的几个时段中,先用UITouch创建一个实例,并调用方法:anyobject 再利用locationInView获取坐标
3.设置连线的时候,要重新调用绘制方法:setNeedDisplay
4.绘制画版 加载到视图中;
转载于:https://my.oschina.net/CeeYang/blog/609455