[iOS学习笔记]自学过程中积累的知识点(二)
来源:互联网 发布:域名加空间 编辑:程序博客网 时间:2024/05/30 05:13
12. UIScrollView
常见的三个属性
- @property(nonatomic) CGPoint contentOffset;
这个属性用来表示UIScrollView滚动的位置 - @property(nonatomic) CGSize contentSize;
这个属性用来表示UIScrollView内容的尺寸,滚动范围(能滚多远) - @property(nonatomic) UIEdgeInsets contentInset;
这个属性能够在UIScrollView的4周增加额外的滚动区域
- @property(nonatomic) CGPoint contentOffset;
常用的三个代理事件
必须遵守UIScrollViewDelegate协议,且设置scrollView的delegate属性为self
该协议中的大部分方法都是以scrollView开头- scrollViewWillBeginDragging:
开始拖拽时调用的方法 - scrollViewDidScroll:
滚动时调用的方法 - scrollViewDidEndDragging:willDecelerate:
停止拖拽时调用的方法
- scrollViewWillBeginDragging:
UIScrollView的缩放
- 设置上面的代理对象
- 设置minimumZoomScale和maximumZoomScale的值
- 实现代理的viewForZoomingInScrollView:方法(直接返回要缩放的视图即可)
UIScrollView无法滚动的解决方法
- 没有设置contentSize
- scrollEnable = NO (应该为YES)
- userInteractionEnable = NO (应该为YES)
- 没有取消autolayout功能(想要scrollview滚动,必须取消)
分页效果
把其pageEnable属性设置为YES,然后配合UIPageControl使用切换页面时的动画效果
通过设置偏移量实现下一页[self.scrollView setContentOffset:CGPointMake(currentPage * width, 0) animated:YES];
此种方法无法设置动画时间[UIView animateWithDuration:0.2f animations:^{
self.scrollView.contentOffset = CGPointMake(currentPage * width, 0);
}];
推荐用这种
13. UIPageControl
UIPageControl常用属性如下:
- 设置总页数:numberOfPages
- 当前显示的页码:currentPage
- 只有一页时,是否需要隐藏页码指示器:hidesForSinglePage
- 当前页码指示器颜色:currentPageIndicatorTintColor
- 当前页码指示器颜色:pageIndicatorTintColor
14. NSTimer & CADisplayLink
14.1 NSTimer定时器
14.1.1 NSTimer定时器的开启
- 使用默认的定时模式(NSDefaultRunLoopMode)
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
这种方式在ScrollView自动滚动时,手动滚动一个文本框会导致ScrollView滚动失效
- 使用NSRunLoopCommonModes模式的定时模式
self.timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
这个定时器创建方法和上述的不一样,但是参数是一样的;
这种方法创建了以后需要手动加入RunLoop,在加入时可以指定Mode,Mode有两个可以选择:NSDefaultRunLoopMode和NSRunLoopCommonModes。
NSDefaultRunLoopMode和第一种效果一样;
NSRunLoopCommonModes可以避免冲突
14.1.2 NSTimer定时器的关闭
通过invalidate方法可以停止定时器的工作,一旦定时器被停止了,就不能再次执行任务。只能再创建一个新的定时器才能执行新的任务
- - (void)invalidate
14.2 CADisplayLink
CADisplayLink默认刷新帧数为60帧
// 创建CADisplayLink, 默认每秒60次CADisplayLink *display = [CADisplayLink displayLinkWithTarget:self selector:@selector(updataImage)];// 将CADisplayLink加入到消息循环中[display addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
15. 协议的定义以及使用
1. 定义协议
- 定义protocol (两种optional[代理对象可不实现]、required[代理对象必须实现])
- 增加代理属性 @property (weak, nonatomic) id<AppInfoViewDelegate> delegate;
- 给代理发消息,调用代理的方法(需要判断代理对象是否实现了该方法,不判断调用后(编译时不会)会报错
注意:定义协议的名称命名[类名+Delegate]、协议方法的命名规范[方法名称需要去掉前缀,并且将自己作为参数]
2. 使用代理
- 声明协议
- 设置代理对象
- 实现协议方法
16. UITableView以及UITableViewCell
使用UITableView必须在视图控制器中实现UITableViewDataSource协议,可以选择实现UITableViewDelegate协议,并使tableview.dataSource=self
UITableViewDataSource协议中的几个基本方法
- -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
返回UITableView的组数(如果不写此方法,那么默认是一组) - -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
返回每组的行数 - -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
返回indexPath.section组indexPath.row行的表格视图 - -(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
设置头部文本 - -(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
设置尾部文本 - -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
通过editingStyle判断当前行是出于插入模式还是出于删除模式([self.tableView setEditing:!self.tableView.editing animated:YES];使用setEditing方法来开启/关闭编辑模式)
- -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
UITableViewDelegate协议中的方法
- -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
可以给指定行设置高度
可以使用self.tableView.rowHeight = 100;
来设置所有行的高度 - -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
每行的点击事件 - -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
每组的头部视图(头尾部视图也可以被tableview重用) - -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
每组的尾部视图 - -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
开启编辑模式时,该行是插入模式还是删除模式
- -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
UITableView的一些属性
- 通过tableHeaderView属性设置tableview的头部视图
self.table.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
- 通过tableFooterView属性设置tableview的底部视图
self.table.tableFooterView = [[UISwitch alloc] init];
- 通过tableHeaderView属性设置tableview的头部视图
UITableViewCell的一些属性
UITableViewCellStyle有四种:
UITableViewCellStyleDefault
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
四种样式分别对应UITableViewCell包含的控件
cell.textLabel ————-> 上图中英雄名称
cell.detailTextLabel ——-> 上图中英雄的介绍
cell.imageView ————> 上图中英雄的图片
不同的样式可显示的子控件也不尽相同UITableViewCell的backgroundView
cell.backgroundView ————-> 正常状态下显示的背景图片
cell.selectedBackgroundView —> 被选中的显示的图片UITableViewCell的附加视图
使用cell.accessoryType可以设置系统内置附加视图,其值有以下几种:
UITableViewCellAccessoryNone
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailButton
效果图依次如下:
使用cell.accessoryView可以设置UI控件为附加视图cell.accessoryView = [[UISwitch alloc] init];
效果如图:
5. tableview的更新方法 更新指定行的数据
Hero *hero = self.heros[row];hero.name = newStr; // 1. 更新表格列对应的数据模型NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];// 2.要更新的对应行// 3. 使用reloadRowsAtIndexPaths:withRowAnimation:方法[self.table reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]; /*******************删除指定行*********************/[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];/*******************插入指定行*********************/ [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];
更新所有行的数据
[self.tableView reloadData]; // 在更新完数据模型之后使用该方法
6. tableview更新数据行时可选的动画:
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) { UITableViewRowAnimationFade, UITableViewRowAnimationRight, // slide in from right (or out to right) UITableViewRowAnimationLeft, UITableViewRowAnimationTop, UITableViewRowAnimationBottom, UITableViewRowAnimationNone, // available in iOS 3.0 UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you};
7. tableview的索引 先上张效果图:![这里写图片描述](http://img.blog.csdn.net/20150527151920346)
#pragma mark - sectionIndex/** * 右边的蓝色索引 */-(NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView{ return [self.carGroups valueForKeyPath:@"title"];}/** * 每个组的头部的标题 */-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ LYYCarGroup *group = self.carGroups[section]; return group.title;}
8. tableview的分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;/** 可选如下 */typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) { UITableViewCellSeparatorStyleNone, UITableViewCellSeparatorStyleSingleLine, UITableViewCellSeparatorStyleSingleLineEtched // This separator style is only supported for grouped style table views currently};
9. 水平/竖直滚动条
self.tableView.showsVerticalScrollIndicator = NO; // 隐藏竖直滚动条self.tableView.showsHorizontalScrollIndicator = YES; // 显示水平滚动条
10. 设置cell能否被选中
self.tableView.allowsSelection = NO; // 不能被选中
11. tableview的滚动
// 让tableveiw滚动到最后一行NSIndexPath *path = [NSIndexPath indexPathForRow:self.messages.count -1 inSection:0];/* * AtIndexPath: 要滚动到哪一行 * atScrollPosition:滚动到哪一行的什么位置 * animated:是否需要滚动动画 */[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];// UITableViewScrollPosition的取值typedef NS_ENUM(NSInteger, UITableViewScrollPosition) { UITableViewScrollPositionNone, UITableViewScrollPositionTop, UITableViewScrollPositionMiddle, UITableViewScrollPositionBottom}; // scroll so row of interest is completely visible at top/center/bottom of view
UITableViewCell以及头(尾)部视图自定义的时候需要注意的一些事项
- 默认TableView的Content属性设置为Static Cells时可实现WYSIWYG的效果
- 默认TableView的Content属性设置为Dynamic Prototypes时可以通过设置一致的identifier从而在TableView中无需手动创建 e.g.:
static NSString *identifier = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];return cell;
这种方式在指出identifier后如果在执行[tableView dequeueReusableCellWithIdentifier:identifier];
后为空,系统会自动去找,如果没有找到就报错了
3. 如果是自定义的Cell无法正常显示的话,可以重写`- (void)layoutSubviews`方法来设置内部控件的frame(但凡在init方法中获取到的frame都是0)4. 当一个控件被添加到其它视图上的时候会调用以下方法 - -(void)didMoveToSuperview - -(void)willMoveToSuperview:(UIView *)newSuperview
17. 状态栏相关
- 设置是否隐藏状态栏
/** * 返回YES则隐藏状态栏,否则不隐藏 */-(BOOL) prefersStatusBarHidden{ return YES;}
- 设置状态栏样式(字体是黑的还是白的)
/** * UIStatusBarStyleDefault为默认样式 * UIStatusBarStyleLightContent字体是白的 */-(UIStatusBarStyle) preferredStatusBarStyle{ return UIStatusBarStyleLightContent;}
18. UIAlertView
处理UIAlertView的事件需要实现UIAlertViewDelegate协议
eg.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ Hero *hero = self.heros[indexPath.row]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"修改数据" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; // 设置AlertView的样式 UITextField *textField = [alert textFieldAtIndex:0]; textField.text = hero.name; [alert show]; // 调用此方法以显示alertView alert.tag = indexPath.row;}#pragma mark - UIAlertViewDelegate-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0) return; UITextField *textField = [alertView textFieldAtIndex:0]; NSString *newStr = textField.text; int row = alertView.tag; Hero *hero = self.heros[row]; hero.name = newStr; NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0]; [self.table reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]; // 更新数据}
alert.alertViewStyle的所有可选值:
typedef NS_ENUM(NSInteger, UIAlertViewStyle) { UIAlertViewStyleDefault = 0, // 默认的提示Label样式 UIAlertViewStyleSecureTextInput, // 密码框样式 UIAlertViewStylePlainTextInput, // Text样式 UIAlertViewStyleLoginAndPasswordInput // 登陆框(账号、密码)样式};
19. 计算一串文字的显示区域的大小
CGSize infoMaxSize = CGSizeMake(300, MAXFLOAT); // 最大显示范围NSDictionary *dict = @{NSFontAttributeName : [UIFont systemFontOfSize:15]}; // 要计算的文本字体// 如果实际显示范围小于设置的最大显示范围,那么返回实际显示范围// 否则,返回最大显示范围CGSize infoSize = [self.weibo.text boundingRectWithSize:infoMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
20. 通知中心(NSNotificationCenter) & 通知(NSNotification)
- 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信
- 任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么。其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知
NSNotification的属性与主要方法
- (NSString *)name; // 通知的名称- (id)object; // 通知发布者(是谁要发布通知)- (NSDictionary *)userInfo; // 一些额外的信息(通知发布者传递给通知接收者的信息内容)+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;
NSNotificationCenter的主要方法
- 获取实例
// 使用defaultCenter方法获取唯一实例NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
- 发布通知
- (void)postNotification:(NSNotification *)notification;// 发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等- (void)postNotificationName:(NSString *)aName object:(id)anObject;// 发布一个名称为aName的通知,anObject为这个通知的发布者- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;// 发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息
- 注册通知监听器
// 通知中心(NSNotificationCenter)提供了方法来注册一个监听通知的监听器(Observer)- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;/** * observer:监听器,即谁要接收这个通知 * aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入 * aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知 * anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知 */- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;/** * name:通知的名称 * obj:通知发布者 * block:收到对应的通知时,会回调这个block * queue:决定了block在哪个操作队列中执行,如果传nil,默认在当前操作队列中同步执行 */
- 取消注册停止监听器
// 通知中心不会保留(retain)监听器对象,在通知中心注册过的对象,必须在该对象释放前取消注册。// 否则,当相应的通知再次出现时,通知中心仍然会向该监听器发送消息。// 因为相应的监听器对象已经被释放了,所以可能会导致应用崩溃// 通知中心提供了相应的方法来取消注册监听器- (void)removeObserver:(id)observer;- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;// 一般在监听器销毁之前取消注册(如在监听器中加入下列代码):- (void)dealloc { //[super dealloc]; 非ARC中需要调用此句 [[NSNotificationCenter defaultCenter] removeObserver:self];}
键盘的通知名称
UIKeyboardWillShowNotification // 键盘即将显示UIKeyboardDidShowNotification // 键盘显示完毕UIKeyboardWillHideNotification // 键盘即将隐藏UIKeyboardDidHideNotification // 键盘隐藏完毕UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕// 伴随着通知发出的一些额外信息(字典):UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frameUIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)
UIDevice通知
// UIDevice类提供了一个单粒对象,它代表着设备,通过它可以获得一些设备相关的信息// 比如电池电量值(batteryLevel)、电池状态(batteryState)// 设备的类型(model,比如iPod、iPhone等)、设备的系统(systemVersion)// 通过[UIDevice currentDevice]可以获取这个单粒对象[UIDevice currentDevice]// UIDevice对象会不间断地发布一些通知,下列是UIDevice对象所发布通知的名称常量:UIDeviceOrientationDidChangeNotification // 设备旋转UIDeviceBatteryStateDidChangeNotification // 电池状态改变UIDeviceBatteryLevelDidChangeNotification // 电池电量改变UIDeviceProximityStateDidChangeNotification // 近距离传感器(比如设备贴近了使用者的脸部)
- [iOS学习笔记]自学过程中积累的知识点(二)
- [iOS学习笔记]自学过程中积累的知识点(一)
- [iOS学习笔记]自学过程中积累的知识点(三)
- [iOS学习笔记]自学过程中积累的知识点(四)
- [iOS学习笔记]自学过程中积累的知识点(五)
- c++学习过程中积累的知识点
- IOS自学笔记Foundation (二)
- 【ios自学笔记】OC中log信息的输出方法,包含import、字符串、输出日志等知识点
- 【前端学习笔记】重要知识点积累
- iOS学习笔记(琐碎的知识点)
- Android知识点积累(二)
- 知识点积累(二)
- ios小知识点积累
- ABAQUS学习过程中遇到的问题积累
- 学习Alchemy过程中积累的一些网站
- Ruby学习过程中积累下来的测试代码
- Ruby学习过程中积累下来的测试代码(进阶)
- 自学opencv过程中轮廓学习时遇到的几个问题
- 字符(串)
- Linux中解压文件
- Google Protocol Buffer
- 【SqlServer】未找到或无法访问服务器.请验证实例名称是否正确并且SQL SERVER已配置允许远程链接
- 结构体
- [iOS学习笔记]自学过程中积累的知识点(二)
- 在Linux下改变进程的优先级. normal_prio
- C++红黑树的完整实现
- C++11 Lambda
- 二进制数逆转,并以十进制输出
- 桥梁模式和适配器模式的区别
- java.lang.OutOfMemoryError: PermGen space
- 每个Android开发者都应该知道的开源库
- 【原创】CGIC库的移植及使用