ios 开发总结(一)

开发知识汇总

关键字const的位置

const意味着”只读”,分析下面的含义

const int a;
int const a;
// 前两个的作用是一样,a是一个常整型数。
const int *a;
// 第三个意味着a是一个指向常整型数的指针(整型数是不可修改的,但指针可以)
int * const a;
// 第四个意思a是一个指向整型数的常指针(指针指向的整型数是可以修改的,但指针是不可修改的)
int const * const a;
// a是一个指向常整型数的常指针(指针指向的整型数是不可修改的,同时指针也是不可修改的)。表示a是一个指针常量,初始化的时候必须固定指向一个int常量或者int变量,之后就不能再指向别的地方了,它总是把它所指向的目标当作一个int常量。也可以写成const int* const a;含义相同。
滑动隐藏导航栏(navigation bar)
navigationController.hidesBarsOnSwipe = YES;
Navigationbar变成透明而不模糊(navigation bar)
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                         forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar .shadowImage = [UIImage new];
self.navigationController.navigationBar .translucent = YES;
仿系统弹出出动画(渐变大)

必须把changeOutView先添加到父视图上去

+(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{ CAKeyframeAnimation * animation; animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = dur;
    animation.delegate = self;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards; NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values; animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [changeOutView.layer addAnimation:animation forKey:nil];
}
关于View
view.layer.shouldRasterize = YES;//去除View锯齿
view.layer.shadowColor //投影颜色
view.layer.shadowOffset //投影偏移量
view.layer.shadowRadius //投影弧度
view.layer.shadowPath //投影路径
#设置View 的圆角等样式都是设置 view.layer 的属性
关于导航栏
//设置导航栏颜色(全局设置)
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
//设置导航栏背景图片
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault]; 
//自定义返回按钮图片(默认状态)
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back_btn.png"]];
//自定义返回按钮图片(按下状态)
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back_btn.png"]]; 
#设置导航返回按钮颜色(除了返回按钮,tintColor属性会影响到所有按钮标题和图片。)
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; 
//修改导航栏的返回按钮颜色
self.navigationController.tintcolor = color
//设置导航栏标题的风格(dictionary    key 
    UITextAttributeFont – 字体key
    UITextAttributeTextColor – 文字颜色key
    UITextAttributeTextShadowColor – 文字阴影色key
    UITextAttributeTextShadowOffset – 文字阴影偏移量key)
    UITextAttributeFont 字体大小
[[UINavigationBar appearance] setTitleTextAttributes:(NSDictionary<NSString *,id> * _Nullable)]
//自定义导航栏View (一般用于设置图片,或者按钮)
self.navigationItem.titleView = view
版本号比较
 if ([string1 compare:@"string2"] == 0) {
  //版本相等        
  }

禁止键盘弹出时UIWebView自动滑动
webview.scrollView.delegate=self;
 -(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView {
   return nil;
 }
有时候layer.cornerRadius并不能满足需求,自己实现drawRect又太麻烦,怎么办?
- (void)dwMakeBottomRoundCornerWithRadius:(CGFloat)radius view:(UIView *)views
{
    CGSize size = views.frame.size;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setFillColor:[[UIColor whiteColor] CGColor]];
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, size.width - radius, size.height);
    CGPathAddArc(path, NULL, size.width-radius, size.height-radius, radius, M_PI/2, 0.0, YES);
    CGPathAddLineToPoint(path, NULL, size.width, 0.0);
    CGPathAddLineToPoint(path, NULL, 0.0, 0.0);
    CGPathAddLineToPoint(path, NULL, 0.0, size.height - radius);
    CGPathAddArc(path, NULL, radius, size.height - radius, radius, M_PI, M_PI/2, YES);
    CGPathCloseSubpath(path);
    [shapeLayer setPath:path];
    CFRelease(path);
    views.layer.mask = shapeLayer;//layer的mask,顾名思义,是种位掩蔽,在shapeLayer的填充区域中,alpha值不为零的部分,self会被绘制;alpha值为零的部分,self不会被绘制,甚至不会响应touch
 }
drawrect 画线
- (void)drawRect:(CGRect)rect  
{  
    CGContextRef context = UIGraphicsGetCurrentContext();  
       
   
       
    /*NO.1画一条线 
       
     CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色 
     CGContextMoveToPoint(context, 20, 20); 
     CGContextAddLineToPoint(context, 200,20); 
     CGContextStrokePath(context); 
    */  
   
       
       
    /*NO.2写文字 
       
    CGContextSetLineWidth(context, 1.0); 
    CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5); 
    UIFont  *font = [UIFont boldSystemFontOfSize:18.0]; 
    [@"公司:北京中软科技股份有限公司\n部门:ERP事业部\n姓名:McLiang" drawInRect:CGRectMake(20, 40, 280, 300) withFont:font]; 
    */  
   
       
    /*NO.3画一个正方形图形 没有边框 
  
    CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5); 
    CGContextFillRect(context, CGRectMake(2, 2, 270, 270)); 
    CGContextStrokePath(context); 
    */  
    
       
    /*NO.4画正方形边框 
      
    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色 
    CGContextSetLineWidth(context, 2.0); 
    CGContextAddRect(context, CGRectMake(2, 2, 270, 270)); 
    CGContextStrokePath(context); 
    */  
   
       
    /*NO.5画方形背景颜色 
       
    CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); 
    CGContextScaleCTM(context, 1.0f, -1.0f); 
    UIGraphicsPushContext(context); 
    CGContextSetLineWidth(context,320); 
    CGContextSetRGBStrokeColor(context, 250.0/255, 250.0/255, 210.0/255, 1.0); 
    CGContextStrokeRect(context, CGRectMake(0, 0, 320, 460)); 
    UIGraphicsPopContext(); 
    */  
   
    /*NO.6椭圆 
       
     CGRect aRect= CGRectMake(80, 80, 160, 100); 
     CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 
     CGContextSetLineWidth(context, 3.0); 
     CGContextAddEllipseInRect(context, aRect); //椭圆 
     CGContextDrawPath(context, kCGPathStroke); 
    */  
   
    /*NO.7 
    CGContextBeginPath(context); 
    CGContextSetRGBStrokeColor(context, 0, 0, 1, 1); 
    CGContextMoveToPoint(context, 100, 100); 
    CGContextAddArcToPoint(context, 50, 100, 50, 150, 50); 
    CGContextStrokePath(context); 
    */  
   
    /*NO.8渐变 
    CGContextClip(context); 
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); 
    CGFloat colors[] = 
    { 
        204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00, 
        29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00, 
        0.0 / 255.0,  50.0 / 255.0, 126.0 / 255.0, 1.00, 
    }; 
    CGGradientRef gradient = CGGradientCreateWithColorComponents 
    (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4)); 
    CGColorSpaceRelease(rgb); 
    CGContextDrawLinearGradient(context, gradient,CGPointMake 
                                (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height), 
                                kCGGradientDrawsBeforeStartLocation); 
     */  
       
      
    /* NO.9四条线画一个正方形 
    //画线 
        UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 
    CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 
       CGContextSetFillColorWithColor(context, aColor.CGColor); 
    CGContextSetLineWidth(context, 4.0); 
    CGPoint aPoints[5]; 
    aPoints[0] =CGPointMake(60, 60); 
    aPoints[1] =CGPointMake(260, 60); 
    aPoints[2] =CGPointMake(260, 300); 
    aPoints[3] =CGPointMake(60, 300); 
    aPoints[4] =CGPointMake(60, 60); 
    CGContextAddLines(context, aPoints, 5); 
    CGContextDrawPath(context, kCGPathStroke); //开始画线 
     */  
       
       
       
    /*  NO.10 
    UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 
    CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 
    CGContextSetFillColorWithColor(context, aColor.CGColor); 
    //椭圆 
    CGRect aRect= CGRectMake(80, 80, 160, 100); 
    CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 
    CGContextSetLineWidth(context, 3.0); 
      CGContextSetFillColorWithColor(context, aColor.CGColor); 
       CGContextAddRect(context, rect); //矩形 
    CGContextAddEllipseInRect(context, aRect); //椭圆 
    CGContextDrawPath(context, kCGPathStroke); 
     */  
   
       
       
    /*  NO.11 
     画一个实心的圆 
   
     CGContextFillEllipseInRect(context, CGRectMake(95, 95, 100.0, 100)); 
    */  
       
       
       
    /*NO.12 
     画一个菱形 
    CGContextSetLineWidth(context, 2.0); 
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
    CGContextMoveToPoint(context, 100, 100); 
    CGContextAddLineToPoint(context, 150, 150); 
    CGContextAddLineToPoint(context, 100, 200); 
    CGContextAddLineToPoint(context, 50, 150); 
    CGContextAddLineToPoint(context, 100, 100); 
    CGContextStrokePath(context); 
     */  
   
    /*NO.13 画矩形 
    CGContextSetLineWidth(context, 2.0); 
  
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  
    CGRect rectangle = CGRectMake(60,170,200,80); 
  
    CGContextAddRect(context, rectangle); 
      
    CGContextStrokePath(context); 
     */  
       
      
    /*椭圆 
    CGContextSetLineWidth(context, 2.0); 
  
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  
    CGRect rectangle = CGRectMake(60,170,200,80); 
  
    CGContextAddEllipseInRect(context, rectangle); 
      
    CGContextStrokePath(context); 
     */  
       
    /*用红色填充了一段路径: 
      
    CGContextMoveToPoint(context, 100, 100); 
    CGContextAddLineToPoint(context, 150, 150); 
    CGContextAddLineToPoint(context, 100, 200); 
    CGContextAddLineToPoint(context, 50, 150); 
    CGContextAddLineToPoint(context, 100, 100); 
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextFillPath(context); 
    */  
       
    /*填充一个蓝色边的红色矩形 
    CGContextSetLineWidth(context, 2.0); 
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
    CGRect rectangle = CGRectMake(60,170,200,80); 
    CGContextAddRect(context, rectangle); 
    CGContextStrokePath(context); 
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextFillRect(context, rectangle); 
    */  
       
    /*画弧 
     //弧线的是通过指定两个切点,还有角度,调用CGContextAddArcToPoint()绘制 
    CGContextSetLineWidth(context, 2.0); 
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
    CGContextMoveToPoint(context, 100, 100); 
    CGContextAddArcToPoint(context, 100,200, 300,200, 100); 
    CGContextStrokePath(context); 
    */  
      
       
    /* 
    绘制贝兹曲线 
    //贝兹曲线是通过移动一个起始点,然后通过两个控制点,还有一个中止点,调用CGContextAddCurveToPoint() 函数绘制 
    CGContextSetLineWidth(context, 2.0); 
  
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  
    CGContextMoveToPoint(context, 10, 10); 
  
    CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400); 
      
    CGContextStrokePath(context); 
     */  
       
    /*绘制二次贝兹曲线 
      
      CGContextSetLineWidth(context, 2.0); 
  
      CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  
      CGContextMoveToPoint(context, 10, 200); 
  
      CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 
      
      CGContextStrokePath(context); 
     */  
       
    /*绘制虚线 
    CGContextSetLineWidth(context, 5.0); 
  
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  
    CGFloat dashArray[] = {2,6,4,2}; 
  
    CGContextSetLineDash(context, 3, dashArray, 4);//跳过3个再画虚线,所以刚开始有6-(3-2)=5个虚点 
      
    CGContextMoveToPoint(context, 10, 200); 
      
    CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 
      
    CGContextStrokePath(context); 
    */  
/*绘制图片 
    NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
    UIImage* myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath]; 
    //[myImageObj drawAtPoint:CGPointMake(0, 0)]; 
    [myImageObj drawInRect:CGRectMake(0, 0, 320, 480)]; 
  
    NSString *s = @"我的小狗"; 
  
    [s drawAtPoint:CGPointMake(100, 0) withFont:[UIFont systemFontOfSize:34.0]]; 
*/  
       
  /* 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
    UIImage *img = [UIImage imageWithContentsOfFile:path]; 
    CGImageRef image = img.CGImage; 
    CGContextSaveGState(context); 
    CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
    CGContextDrawImage(context, touchRect, image); 
    CGContextRestoreGState(context); 
   */  
     
       
    /*NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
    UIImage *img = [UIImage imageWithContentsOfFile:path]; 
    CGImageRef image = img.CGImage; 
    CGContextSaveGState(context); 
  
    CGContextRotateCTM(context, M_PI); 
    CGContextTranslateCTM(context, -img.size.width, -img.size.height); 
  
    CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
    CGContextDrawImage(context, touchRect, image); 
    CGContextRestoreGState(context);*/  
   
/* 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
    UIImage *img = [UIImage imageWithContentsOfFile:path]; 
    CGImageRef image = img.CGImage; 
      
    CGContextSaveGState(context); 
  
    CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI); 
    myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height); 
    CGContextConcatCTM(context, myAffine); 
  
    CGContextRotateCTM(context, M_PI); 
    CGContextTranslateCTM(context, -img.size.width, -img.size.height); 
  
    CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
    CGContextDrawImage(context, touchRect, image); 
    CGContextRestoreGState(context); 
*/  
}  

判断UITableView滚动是否到底

- (void)scrollViewDidScroll:(UIScrollView*)aScrollView {      CGPointoffset = aScrollView.contentOffset; 
  CGRectbounds = aScrollView.bounds;  
   CGSizesize = aScrollView.contentSize;
   UIEdgeInsetsinset = aScrollView.contentInset;
   floaty = offset.y+ bounds.size.height- inset.bottom;  
   floath = size.height;  
   floatreload_distance = -20; 
    if(y > h + reload_distance) {
       //距离到底20像  素    
  }     
 }
自定义了leftBarbuttonItem左滑返回手势失效
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
                                        initWithImage:img
                                        style:UIBarButtonItemStylePlain
                                        target:self
                                        action:@selector(onBack:)];
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

判断NSArray倒序

NSArray *tmparr = [[upLoadImageArr reverseObjectEnumerator] allObjects];
 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容