IOS开发基础知识--碎片47
1:解决ios静态库中的类别(category)在工程中不能使用
解决方法为:找到 target 的图标,更改其 Other Linker Flags 为: -all_load 或 -force_load
-force_load,后跟随一个文件位置,可以更精确地加载所需文件。
简单点说就是,Objective-C 的动态特性使得需要,为链接器添加一个标签(设置 Other Linker Flags 为 -ObjC)来解决通过 Category 向类添加方法的问题。
但这个标签 -ObjC 在 64 位 和 iOS 中有问题,需要使用 -all_load 或 -force_load。
总结如下:
如果,第三库中没有 category,Other Linker Flags 无需设置
如果,第三方库中有 category,需要设置为 -ObjC
如果,某些 Xcode 版本中,出现问题,修改设置为 -all_load
2:画虚线的两种方式
a:重写drawRect - (void)drawRect:(CGRect)rect{ [super drawRect:rect]; CGContextRef currentContext = UIGraphicsGetCurrentContext(); //设置虚线颜色 CGContextSetStrokeColorWithColor(currentContext, [UIColor BlackColor].CGColor); //设置虚线宽度 CGContextSetLineWidth(currentContext, 1); //设置虚线绘制起点 CGContextMoveToPoint(currentContext, 0, 0); //设置虚线绘制终点 CGContextAddLineToPoint(currentContext, self.frame.origin.x + self.frame.size.width, 0); //设置虚线排列的宽度间隔:下面的arr中的数字表示先绘制3个点再绘制1个点 CGFloat arr[] = {3,1}; //下面最后一个参数“2”代表排列的个数。 CGContextSetLineDash(currentContext, 0, arr, 2); CGContextDrawPath(currentContext, kCGPathStroke); }
b:通过UIImage的绘图方法来绘制 // 画虚线 // 创建一个imageView 高度是你想要的虚线的高度 一般设为2 _lineImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, 2)]; // 调用方法 返回的iamge就是虚线 _lineImg.image = [self drawLineByImageView:_lineImg]; // 添加到控制器的view上 [self.view addSubview:_lineImg]; // 返回虚线image的方法 - (UIImage *)drawLineByImageView:(UIImageView *)imageView{ UIGraphicsBeginImageContext(imageView.frame.size); //开始画线 划线的frame [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)]; //设置线条终点形状 CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); // 5是每个虚线的长度 1是高度 float lengths[] = {5,1}; CGContextRef line = UIGraphicsGetCurrentContext(); // 设置颜色 CGContextSetStrokeColorWithColor(line, [UIColor colorWithWhite:0.408 alpha:1.000].CGColor); CGContextSetLineDash(line, 0, lengths, 2); //画虚线 CGContextMoveToPoint(line, 0.0, 2.0); //开始画线 CGContextAddLineToPoint(line, kScreenWidth - 10, 2.0); CGContextStrokePath(line); // UIGraphicsGetImageFromCurrentImageContext()返回的就是image return UIGraphicsGetImageFromCurrentImageContext(); }
3:CGContextRef介绍
Graphics Context是图形上下文,也可以理解为一块画布,我们可以在上面进行绘画操作,绘制完成后,将画布放到我们的view中显示即可,view看作是一个画框.
1:写文字 - (void)drawRect:(CGRect)rect { //获得当前画板 CGContextRef ctx = UIGraphicsGetCurrentContext(); //颜色 CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0); //画线的宽度 CGContextSetLineWidth(ctx, 0.25); //开始写字 [@"我是文字" drawInRect:CGRectMake(10, 10, 100, 30) withFont:font]; [super drawRect:rect]; } 2:画直线 - (void)drawRect:(CGRect)rect { //获得当前画板 CGContextRef ctx = UIGraphicsGetCurrentContext(); //颜色 CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0); //画线的宽度 CGContextSetLineWidth(ctx, 0.25); //顶部横线 CGContextMoveToPoint(ctx, 0, 10); CGContextAddLineToPoint(ctx, self.bounds.size.width, 10); CGContextStrokePath(ctx); [super drawRect:rect]; } 3:画圆 - (void)drawRect:(CGRect)rect { //获得当前画板 CGContextRef ctx = UIGraphicsGetCurrentContext(); //颜色 CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0); //画线的宽度 CGContextSetLineWidth(ctx, 0.25); //void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度 // x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。 CGContextAddArc(ctx, 100, 20, 20, 0, 2*M_PI, 0); //添加一个圆 CGContextDrawPath(ctx, kCGPathStroke); //绘制路径 [super drawRect:rect]; } 4:画矩形 - (void)drawRect:(CGRect)rect { //获得当前画板 CGContextRef ctx = UIGraphicsGetCurrentContext(); //颜色 CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0); //画线的宽度 CGContextSetLineWidth(ctx, 0.25); CGContextAddRect(ctx, CGRectMake(2, 2, 30, 30)); CGContextStrokePath(ctx); [super drawRect:rect]; }
4:判断当前ViewController是push还是present的方式显示的
NSArray *viewcontrollers=self.navigationController.viewControllers; if (viewcontrollers.count > 1) { if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self) { //push方式 [self.navigationController popViewControllerAnimated:YES]; } } else { //present方式 [self dismissViewControllerAnimated:YES completion:nil]; }
5:获取实际使用的LaunchImage图片
- (NSString *)getLaunchImageName { CGSize viewSize = self.window.bounds.size; // 竖屏 NSString *viewOrientation = @"Portrait"; NSString *launchImageName = nil; NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"]; for (NSDictionary* dict in imagesDict) { CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]); if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) { launchImageName = dict[@"UILaunchImageName"]; } } return launchImageName; }
6:判断对象是否遵循了某协议
if ([self.selectedController conformsToProtocol:@protocol(RefreshPtotocol)]) { [self.selectedController performSelector:@selector(onTriggerRefresh)]; }
7:判断view是不是指定视图的子视图
BOOL isView = [textView isDescendantOfView:self.view];
8:阿拉伯数字转中文格式
+(NSString *)translation:(NSString *)arebic { NSString *str = arebic; NSArray *arabic_numerals = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"]; NSArray *chinese_numerals = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"]; NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chinese_numerals forKeys:arabic_numerals]; NSMutableArray *sums = [NSMutableArray array]; for (int i = 0; i < str.length; i ++) { NSString *substr = [str substringWithRange:NSMakeRange(i, 1)]; NSString *a = [dictionary objectForKey:substr]; NSString *b = digits[str.length -i-1]; NSString *sum = [a stringByAppendingString:b]; if ([a isEqualToString:chinese_numerals[9]]) { if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]]) { sum = b; if ([[sums lastObject] isEqualToString:chinese_numerals[9]]) { [sums removeLastObject]; } }else { sum = chinese_numerals[9]; } if ([[sums lastObject] isEqualToString:sum]) { continue; } } [sums addObject:sum]; } NSString *sumStr = [sums componentsJoinedByString:@""]; NSString *chinese = [sumStr substringToIndex:sumStr.length-1]; NSLog(@"%@",str); NSLog(@"%@",chinese); return chinese; }
9:如何获取WebView所有的图片地址
//UIWebView - (void)webViewDidFinishLoad:(UIWebView *)webView { //这里是js,主要目的实现对url的获取 static NSString * const jsGetImages = @"function getImages(){\ var objs = document.getElementsByTagName(\"img\");\ var imgScr = '';\ for(var i=0;i<objs.length;i++){\ imgScr = imgScr + objs[i].src + '+';\ };\ return imgScr;\ };"; [webView stringByEvaluatingJavaScriptFromString:jsGetImages];//注入js方法 NSString *urlResult = [webView stringByEvaluatingJavaScriptFromString:@"getImages()"]; NSArray *urlArray = [NSMutableArray arrayWithArray:[urlResult componentsSeparatedByString:@"+"]]; //urlResurlt 就是获取到得所有图片的url的拼接;mUrlArray就是所有Url的数组 }
//WKWebView - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation { static NSString * const jsGetImages = @"function getImages(){\ var objs = document.getElementsByTagName(\"img\");\ var imgScr = '';\ for(var i=0;i<objs.length;i++){\ imgScr = imgScr + objs[i].src + '+';\ };\ return imgScr;\ };"; [webView evaluateJavaScript:jsGetImages completionHandler:nil]; [webView evaluateJavaScript:@"getImages()" completionHandler:^(id _Nullable result, NSError * _Nullable error) { NSLog(@"%@",result); }]; }
10:navigationBar根据滑动距离的渐变色实现
//第一种 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetToShow = 200.0;//滑动多少就完全显示 CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow; [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = alpha; }
//第二种 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetToShow = 200.0; CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow; [self.navigationController.navigationBar setShadowImage:[UIImage new]]; [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor orangeColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault]; } //生成一张纯色的图片 - (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; }
11:iOS 开发中一些相关的路径
模拟器的位置: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs 文档安装位置: /Applications/Xcode.app/Contents/Developer/Documentation/DocSets 插件保存路径: ~/Library/ApplicationSupport/Developer/Shared/Xcode/Plug-ins 自定义代码段的保存路径: ~/Library/Developer/Xcode/UserData/CodeSnippets/ 如果找不到CodeSnippets文件夹,可以自己新建一个CodeSnippets文件夹。 描述文件路径 ~/Library/MobileDevice/Provisioning Profiles