iOS项目中常用的代码总结

2018-11-07  本文已影响0人  Space2016

人到中年,总想着拷贝代码


UI相开始

//重用ID
static NSString * Main_SetCellID = @"Main_SetCell";
//初始化
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(self.topView.frame), self.view.width, self.view.height - self.topView.height) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[tableView registerNib:[UINib nibWithNibName:NSStringFromClass([Main_SetCell class]) bundle:nil] forCellReuseIdentifier:Main_SetCellID];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.backgroundColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0];
[self.view addSubview:tableView];
//常用的代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray * c = self.sections[section];
    return c.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Main_SetCell *cell = [tableView dequeueReusableCellWithIdentifier:Main_SetCellID];
    //cell 选中背景样式
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = VI_GlobalBlack0;
    NSMutableArray * datas = self.sections[indexPath.section];
    Main_SetCellBaseModel * model = datas[indexPath.row];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 30;
    }
    return 64;
}
//headerView
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    Main_THeaderView * view = [[Main_THeaderView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.width,64)];
    if (section == 0)
    {
        return nil;
    }
    else if(section == 1)
    {
        view.label.text = @"DEVICE SETTINGS";
        return view;
    }
    else
    {
        view.label.text = @"VIDEO SETTINGS";
        return view;
    }
}
 UICollectionViewFlowLayout *flowLayoutOne = [[UICollectionViewFlowLayout alloc] init];
 flowLayoutOne.scrollDirection = UICollectionViewScrollDirectionHorizontal;
 flowLayoutOne.minimumLineSpacing = 0;//设置最小行间距
 flowLayoutOne.minimumInteritemSpacing = 0;//item间距(最小值)
 flowLayoutOne.sectionInset = UIEdgeInsetsMake(0, 20, 0, 0);
 UICollectionView * cView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(container.frame) + 2, ScreenW,80) collectionViewLayout:flowLayoutOne];
 cView.dataSource = self;
 cView.delegate = self;
 [cView registerNib:[UINib nibWithNibName:NSStringFromClass([VI_ME_Editor_SetCCell class]) bundle:nil] forCellWithReuseIdentifier:VI_ME_Editor_SetCCellID];
 cView.showsHorizontalScrollIndicator = NO;
 [self.view addSubview:cView];
 self.cView = cView;
UILabel * titleLabel = [[UILabel alloc] init];
titleLabel.text = NSLocalizedString(@"XXXX",nil);
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont systemFontOfSize:15];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.frame = CGRectMake(0,0,frame.size.width,50);
[self addSubview:titleLabel];
self.titleLabel = titleLabel;

自定义navigationBar 左右的item

UIButton * rightButton  = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 50)];
rightButton.backgroundColor = SPSRandomColor;
rightButton.titleLabel.font = [UIFont systemFontOfSize:18];
[rightButton setTitleColor: [UIColor colorWithRed:20/255.0 green:110/255.0 blue:211/255.0 alpha:1.0]
                      forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(AlbumClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightItem;

自定义navigationBar 中间的titleView

UILabel *titleLabel  = [[UILabel alloc]init];
titleLabel.font = [UIFont systemFontOfSize:17];
titleLabel.textColor = [UIColor whiteColor];
self.navigationItem.titleView = titleLabel;

控制器重写这个方法隐藏状态栏

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

画点

//画点其实就是画一个实心的椭圆
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor colorWithRed:0.0/255.0 green:143/255.0 blue:255.0/255.0 alpha:255.0/255.0] setFill];
CGFloat  dotSize = 6.0f;
CGRect dotFrame = CGRectMake(0,0 ,dotSize,dotSize);
CGContextAddEllipseInRect(context, dotFrame);
CGContextFillPath(context);

画线

//点坐标
CGFloat x1 = 0.0 ,y1 = 0.0;
CGFloat x2 = 50.0,y2 = 50.0;
CGContextRef context = UIGraphicsGetCurrentContext();
//颜色
[[UIColor colorWithRed:0.0/255.0 green:143/255.0 blue:255.0/255.0 alpha:255.0/255.0] setStroke];
//线宽
CGContextSetLineWidth(context,5.0);
//设置连接样式
CGContextSetLineJoin(context, kCGLineJoinBevel);
//设置顶角样式
CGContextSetLineCap(context, kCGLineCapButt);
//添加线的第一个点
CGContextMoveToPoint(context,x1,y1);
 //添加线的第二个端点
CGContextAddLineToPoint(context,x2,y2);
//画线
CGContextStrokePath(context);

画空心三角形

CGFloat x1 = 100.0,y1 = 100.0;
CGFloat x2 = 150.0,y2 = 100.0;
CGFloat x3 = 150.0,y3 = 150.0;
CGContextRef context = UIGraphicsGetCurrentContext();
//颜色
[[UIColor colorWithRed:0.0/255.0 green:143/255.0 blue:255.0/255.0 alpha:255.0/255.0] setStroke];
//线宽
CGContextSetLineWidth(context,5.0);
//设置连接样式
CGContextSetLineJoin(context, kCGLineJoinRound);
//设置顶角样式,要不会尖尖的。
CGContextSetLineCap(context, kCGLineCapRound);
//添加线的第一个点
CGContextMoveToPoint(context,x1,y1);
//添加线的第二个端点并画线
CGContextAddLineToPoint(context,x2,y2);
//添加线的第三个端点并画线
CGContextAddLineToPoint(context,x3,y3);
//闭合三角形
CGContextAddLineToPoint(context,x1,y1);
//画图
CGContextStrokePath(context);

实心三角形

CGFloat x1 = 100.0,y1 = 100.0;
CGFloat x2 = 150.0,y2 = 100.0;
CGFloat x3 = 150.0,y3 = 150.0;
CGContextRef context = UIGraphicsGetCurrentContext();
//颜色
[[UIColor colorWithRed:0.0/255.0 green:143/255.0 blue:255.0/255.0 alpha:255.0/255.0] setFill];
//添加线的第一个点
CGContextMoveToPoint(context,x1,y1);
//添加线的第二个端点并画线
CGContextAddLineToPoint(context,x2,y2);
//添加线的第三个端点并画线
CGContextAddLineToPoint(context,x3,y3);
//闭合三角形
CGContextAddLineToPoint(context,x1,y1);
//画图
CGContextFillPath(context);

长方形
多边形
画文字
画图片

长按手势

UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(annLongPress:)];
    longPressGesture.minimumPressDuration = 1.5;
[view addGestureRecognizer:longPressGesture];


UI相关结束





常用的宏定义开始

//weakself
#define WEAKSELF __weak typeof(self) weakSelf = self
//回到main线程
#define SPS_DISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
//rgb颜色
#define SPSColor(r,g,b)  [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
//随机颜色
#define SPSRandomColor   SPSColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
//屏幕宽
#define ScreenW [UIScreen mainScreen].bounds.size.width
//屏幕高
#define ScreenH [UIScreen mainScreen].bounds.size.height
//全局唯一的window
#define KEY_WINDOW  [[UIApplication sharedApplication] keyWindow]
/* 打印frame  */
#define NSLogRect(rect) NSLog(@"%s x:%.2f,y:%.2f,w:%.2f,h:%.2f",#rect,rect.origin.x,rect.origin.y,rect.size.width,rect.size.height)
/* 打印size  */
#define NSLogSize(size) NSLog(@"%s w:%.2f h:%.2f",#size,size.width,size.height)
/* 打印point  */
#define NSLogPoint(point) NSLog(@"%s x:%.2f,y:%.2f",#point,point.x,point.y)



常用的宏定义结束





坐标相关开始

把BView中的CView 转换到AView的坐标。B和C必须是父子空间关系
[A convertRect:B.frame fromView:C.videoBtn];
//
CGPoint 是否在CGRect 中
 (CGRectContainsPoint(CGRect, CGPoint))


坐标相关结束





others

block书写方法
@property(nonatomic,copy)void(^goback)(void);
typedef NSInteger(^goback)(NSMutableArray * array);
//返回值为int 的block
self.block = ^(NSMutableArray *a){
        return 1;
    };
忽略警告
忽略过期的警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic pop
定时器添加和移除
//移除Timer定时器
-(void)removeTimer
{
    [self.timer invalidate];
    self.timer = nil;
}
//添加Timer定时器
-(void)addTimer
{
    if (self.timer != nil)return;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerBegin:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

//地图坐标CLLocationCoordinate2D储存到NSArray

//存储
    NSMutableArray * tempA = [NSMutableArray array];
    NSValue * Vcoo = [NSValue valueWithBytes:&coordinate objCType:@encode(CLLocationCoordinate2D)];
    [tempA addObject:Vcoo];
    
    //读取
    NSValue * va = tempA.firstObject;
    CLLocationCoordinate2D valueCoord;
    [va getValue:&valueCoord];
上一篇 下一篇

猜你喜欢

热点阅读