iOS:小技巧(转)
记录下一些不常用技巧,以防忘记,复制用。
1、获取当前的View在Window的frame:
|
1
2
|
UIWindow * window=[[[UIApplication sharedApplication] delegate] window]; CGRect rect=[_myButton convertRect:_myButton.bounds toView:window]; |
2、UIImageView 和UILabel 等一些控件,需要加这句才能setCorn
|
1
|
_myLabel.layer.masksToBounds = YES; |
3、手机上的沙盒路径要加"Documents",不然存储写入失败!mac上不用!
|
1
2
3
|
[_myArray writeToFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"shopCategory.plist"] atomically:YES]; NSArray *tempAllData = [NSArray arrayWithContentsOfFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"shopCategory.plist"]]; |
后续补充:
Document:一般需要持久的数据都放在此目录中,可以在当中添加子文件夹,iTunes备份和恢复的时候,会包括此目录。
Library:设置程序的默认设置和其他状态信息
temp:创建临时文件的目录,当iOS设备重启时,文件会被自动清除
4、图片拉伸不失真,如聊天软件对话气泡
1)、方法1,比较老的,
|
1
2
|
UIImage *tempImage2 = [UIImage imageNamed:@"sub.png"];tempImage2 = [tempImage2 stretchableImageWithLeftCapWidth:tempImage2.size.width/2 topCapHeight:0]; |
2)、方法2,比较新的
|
1
2
3
4
5
6
7
8
|
UIImage *tempImage3 = [UIImage imageNamed:@"sub"];CGFloat tempH = tempImage3.size.height/2;CGFloat tempW = tempImage3.size.width/2; UIEdgeInsets tempEdg = UIEdgeInsetsMake(tempH, tempW, tempH, tempW); tempImage3 = [tempImage3 resizableImageWithCapInsets:tempEdg resizingMode:UIImageResizingModeStretch]; |
5、视频截取缩略图,其中CMTimeMakeWithSeconds(5,1),调整截图帧数/秒数,一般不用特意去修改,不做参数传入,除非片头一段时间都一样的视频。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#import <AVFoundation/AVFoundation.h>-(UIImage *)getThumbnailImage:(NSString *)videoURL{ AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil]; AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset]; gen.appliesPreferredTrackTransform = YES; //控制截取时间 CMTime time = CMTimeMakeWithSeconds(5, 1); NSError *error = nil; CMTime actualTime; CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error]; UIImage *thumb = [[UIImage alloc] initWithCGImage:image]; CGImageRelease(image); return thumb;} |
6、cell下划线左边顶住屏幕左边。
|
1
2
3
|
cell.preservesSuperviewLayoutMargins = NO;cell.layoutMargins = UIEdgeInsetsZero;cell.separatorInset = UIEdgeInsetsZero; |
7、去除xcode8冗余信息,虽然已经记住了。
OS_ACTIVITY_MODE disable
8、播放音频
1)工程内音频
1-1)、获取音频路径
|
1
2
3
|
NSString *path = [[NSBundle mainBundle] pathForResource:@"shakebell" ofType:@"wav"]; NSURL *url = [NSURL fileURLWithPath:path]; |
1-2)、创建音频播放ID
|
1
2
|
SystemSoundID soundID;AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID); |
1-3)、Play
|
1
|
AudioServicesPlaySystemSound(soundID); |
2)系统音频,参数为1000-1351,具体查表,如1007为“sms-received1”
|
1
|
AudioServicesPlaySystemSound(1007); |
9、字体自适应
1)、固定的Frame,自适应Font大小,如数量增减,1和1000。
|
1
|
[label1 setAdjustsFontSizeToFitWidth:YES]; |
2)、固定的Font,自适应Frame,用于信息类显示
|
1
|
[label2 sizeToFit]; |
3)、固定的Font,获取自适应Frame值,反过来设置Label的Frame,用于信息类显示。这里的100是等下设置Label的width,也是返回的rect.frame.size.width
|
1
|
CGRect rect = [templabel.text boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:templabel.font} context:nil]; |
10、AFNetworking 检测网络连接状态
|
1
2
3
4
5
|
[[AFNetworkReachabilityManager sharedManager]startMonitoring];[[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"%ld",status); }]; |
11、编辑相关
1)键盘事件通知
1-1)、弹出键盘可能盖住TextField。监听键盘的通知
|
1
|
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moveView:) name:UIKeyboardDidChangeFrameNotification object:nil]; |
1-2)、moveView方法里接收通知,tempTime是键盘动画时间,tempY是键盘当前的y轴位置。(接着要移动评论框或者移动后面的ScrollView都可以)
|
1
2
3
4
5
6
7
8
9
10
11
|
CGFloat tempTime = [[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];CGFloat tempY = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].origin.y//重置约束条件//self.theBottomSpace.constant = ?;[UIView animateWithDuration:duration animations:^{ //更新约束 [self.view layoutIfNeeded]; }]; |
2)dealloc记得移除
|
1
|
[[NSNotificationCenter defaultCenter]removeObserver:self]; |
3)touchesBegan:withEvent && scrollViewDidScroll -->屏幕点击&&屏幕滑动要取消编辑状态
|
1
|
[self.view endEditing:YES]; |
12、上传图片(头像)
1-1)、把Image打成NSData
|
1
|
NSData *imagedata = UIImageJPEGRepresentation(tempImage, 1.0); |
1-2)、AFNetworking的POST方法填如下。formData:POST方法里的Block参数,name:跟服务器有关,filename:随意填,mimeType:就image/jpg。
|
1
|
[formData appendPartWithFileData:imagedata name:@"imgFile" fileName:@"idontcare.jpg" mimeType:@"image/jpg"]; |
13、强制布局
|
1
|
[self.view layoutIfNeeded]; |
14、图片双击缩放
1)scrollView才可以缩放,所以要把ImageView加在scrollView,给scrollView(这里的 self )添加手势识别。要设最大/小缩放比例!
|
1
2
3
4
5
|
UITapGestureRecognizer *imageTwoTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twoTapAction:)];imageTwoTap.numberOfTapsRequired = 2;[self addGestureRecognizer: imageTwoTap]; |
2)第一次点哪放大哪,第二次恢复原来大小
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#define SCALE_WIDTH 60 //要放大的局部宽度大小#define SCALE_HEIGHT 60 //要放大的局部高度大小-(void)twoTapAction:(UITapGestureRecognizer *)tempTap{ if (self.zoomScale != 1.0) { [self setZoomScale:1.0 animated:YES]; } else { CGPoint tempPoint = [tempTap locationInView:self]; [self zoomToRect:CGRectMake(tempPoint.x-SCALE_WIDTH/2, tempPoint.y-SCALE_HEIGHT/2, SCALE_WIDTH, SCALE_HEIGHT) animated:YES]; }} |
15、加载XIB
1)、从多个cell样式的XIB加载。只有1个cell样式,可直接lastObject加载。(先根据不同的ID取,取不到再加载。)
1-1)、获取XIB里的所有对象
|
1
|
NSArray *cellArry = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyTableCell class]) owner:self options:nil]; |
1-2)、读取对应的Cell样式,此时的参数type为枚举,或基本数据类型。
|
1
|
cell = [cellArry objectAtIndex:type]; |
2)、在 UIView + xxx 的类别文件里,可以添加这个类。方便加载单种Cell样式的XIB。
|
1
2
3
4
|
+ (instancetype)viewFromXib{ return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];} |
16、常用延时执行
1)、dispatch_after
dispatch_after 代码块,一敲就出来,好像是Xcode8之后的。
2)、performSelector
-(void)performSelector: withObject: afterDelay: ;
iOS:小技巧(转)的更多相关文章
- iOS小技巧总结,绝对有你想要的
原文链接 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIV ...
- iOS小技巧 - 和屏幕等宽的Table分割线
前言 因为本人也是学习iOS才一个多月,在写程序的过程中经常会遇到一些看似应该很简单,但是要解决好却要知道一点小trick的问题. 因此后面会陆续记一些这类问题,一来加深印象,二来也可以做个备忘录. ...
- iOS小技巧:用runtime 解决UIButton 重复点击问题
http://www.cocoachina.com/ios/20150911/13260.html 作者:uxyheaven 授权本站转载. 什么是这个问题 我们的按钮是点击一次响应一次, 即使频繁的 ...
- iOS小技巧3
将颜色合成图片 将颜色合成图片 +(UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1 ...
- iOS小技巧2
这段代码是实现了类似QQ空间"我的空间"里面的圆形头像 //圆形的头像 UIImageView * headImage = [[UIImageView alloc]initWith ...
- 总有你需要的之 ios 小技巧 (下)
图片上绘制文字 写一个UIImage的category NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultPara ...
- IOS小技巧——使用FMDB时如何把一个对像中的NSArray数组属性存到表中
http://blog.csdn.net/github_29614995/article/details/46797917 在开发的当中,往往碰到要将数据持久化的时候用到FMDB,但是碰到模型中的属性 ...
- 你想要的iOS 小技巧总结
UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , ...
- IOS小技巧整理
1 随机数的使用 头文件的引用 #import <time.h> #import <mach/mach_time.h> srandom()的使用 ...
- <iOS小技巧> 昵称格式判断
一.使用方式 + 如下代码块功能:判断字体,判断字体输入格式 NSString *firstStr = [name substringToIndex:1]; NSArray *num ...
随机推荐
- FreeMaker实现变量求和
今天在项目上遇到统计分页页面的某个字段的总和,前台页面是使用FreeMaker实现的,记录一下: <#assign tprice = 0 > <#list orderlist ...
- C语言深度挖掘
二级指针和回调函数的用法: #include <stdio.h> #include <stdlib.h> int add(int num1 ,int num2){ return ...
- JavaScript DOM编程艺术读书笔记(一)
第一章,第二章 DOM:是一套对文档的内容进行抽象和概念化的方法. W3C中的定义:一个与系统平台和编程语言无关的接口,程序和脚本可以通过这个接口动态的访问和修改文档的内容,结构和样式. DHTML( ...
- Python数据库备份脚本
Python数据库备份脚本 #!/usr/bin/env python # author: liudong # -*- coding: utf-8 -*- # filename: db_bak.py ...
- MySQL常见错误及其解决办法
1.连接类 (1).问题:MySQL server has gone away 解决办法:出现该报错常见的原因是服务器超时了并且关闭了连接.缺省地,如果没有事情发生,服务器在 8个小时后关闭连接.如 ...
- javascript篇-----函数作用域,函数作用域链和声明提前
在一些类似C语言的编程语言中,花括号内的每一段代码都具有各自的作用域,而且变量在声明它们的代码段之外是不可见的(也就是我们不能在代码段外直接访问代码段内声明的变量),我们称之为块级作用域,然而,不同于 ...
- history对象的一些知识点
history对象可以保存用户的上网的历史记录,即从窗口被打开的那一刻算起.这里有个比较纠结的问题,出于安全因素的考虑,开发人员无法得知用户浏览过的URL, 只能通过用户访问过的页面列表,实现后退和前 ...
- SQL基本语句以及示例
基本语句: /*dorp colunm*/ 语法:ALTER TABLE 表名 DROP COLUMN 要删除的字段 验证财务转换的正确性,查询以下两个表是否有数据 /*表连接inner jion ...
- JS编码解码
一.定义和用法 encodeURI() 函数可把字符串作为 URI 进行编码. 语法 encodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 URI 或其他要 ...
- 查看oracle表中列的数据类型
一. SQLPLUS中,直接用 DESC[ribe] tablename 即可. 二.在外部应用程序调用查看ORACLE中的表结构时,只能用下面的语句代替: 1.看字段名与数据类型 select * ...