iOS 基础开发技巧 (一)

本文分享了iOS开发中实用的技巧和解决方案,包括如何利用pch文件进行全局配置、解决UITableView Group样式下的顶部空白问题、UIView的frame快速调整、获取任意View对应的ViewController、清理NSUserDefaults数据的方法及GCD定时器的实现。

欢迎大家关注我的公众号,我会定期分享一些我在项目中遇到问题的解决办法和一些iOS实用的技巧,现阶段主要是整理出一些基础的知识记录下来

文章也会同步更新到我的博客:
ppsheep.com

这里主要讲一些我在日常开发中用到的一些小的技巧,其实也算不上技巧吧,就是省去一些不必要的代码,或者有的小问题困扰你很久说不行在这里你能找到答案

iOS OC项目的pch文件使用

在项目中如果我们需要一些公共的引用,或者一些全局的宏 那我们经常在pch中设置好

具体怎么设置呢 在项目下新建一个pch文件

1

一般我会取名 项目名-PrefixHeader

在target——>Bulid Setting 中 设置 PrefixHeader

2

我的项目文件夹结构

$(SRCROOT)这个是指工程的根目录

找到这个pch文件就行 然后启动APP就会编译这个文件了

pch.h中

//
//  APP-1-PrefixHeader.pch
//  APP-1
//
//  Created by 羊谦 on 2016/10/28.
//  Copyright © 2016年 羊谦. All rights reserved.
//

#ifndef APP_1_PrefixHeader_pch
#define APP_1_PrefixHeader_pch

//在这里直接定义你的宏变量   或者公共引用就行

#endif /* APP_1_PrefixHeader_pch */复制代码

UITableView的Group样式下顶部空白处理

要给tableHeaderView赋一个高度不为0的view才能处理顶部留白

//分组列表头部空白处理
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.1)];
self.tableView.tableHeaderView = view;复制代码

在UIView的扩展 快速修改frame

在iOS修改view的frame,我们经常需要写一大堆代码,来修改frame中的一个小属性,这里有一个方法,就是直接修改frame的每个值

新建一个category UIView+PPSFrame.h

#import 

@interface UIView (PPSFrame)

@property (assign, nonatomic) CGFloat    top;//上 相当于frame.origin.y
@property (assign, nonatomic) CGFloat    bottom;//下 相当于frame.size.height + frame.origin.y
@property (assign, nonatomic) CGFloat    left;//相当于frame.origin.x
@property (assign, nonatomic) CGFloat    right;//相当于frame.origin.x+frame.size.width

@property (assign, nonatomic) CGFloat    centerX;
@property (assign, nonatomic) CGFloat    centerY;

@property (assign, nonatomic) CGFloat    width;
@property (assign, nonatomic) CGFloat    height;
@property (assign, nonatomic) CGSize    size;


@end复制代码

在.m文件中设置各个属性

#import "UIView+Layout.h"

@implementation UIView (Layout)

@dynamic top;
@dynamic bottom;
@dynamic left;
@dynamic right;

@dynamic width;
@dynamic height;

@dynamic size;

- (CGFloat)top
{
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)top
{
    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;
}

- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)left
{
    CGRect frame = self.frame;
    frame.origin.x = left;
    self.frame = frame;
}

- (CGFloat)bottom
{
    return self.frame.size.height + self.frame.origin.y;
}

- (void)setBottom:(CGFloat)bottom
{
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)right
{
    return self.frame.size.width + self.frame.origin.x;
}

- (void)setRight:(CGFloat)right
{
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

@end复制代码

获取某个view的Controller

其实就是根据view的响应链,来查找viewcontroller

- (UIViewController *)viewController
{
  UIViewController *viewController = nil;  
  UIResponder *next = self.nextResponder;
  while (next)
  {
    if ([next isKindOfClass:[UIViewController class]])
    {
      viewController = (UIViewController *)next;      
      break;    
    }    
    next = next.nextResponder;  
  } 
    return viewController;
}复制代码

清空NSUserDefaults的记录

方法一:是获取当前的app的bundleId NSUserDefaults中有方法根据bundleId清空记录

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];复制代码

方法二:获取所有存储在NSUserDefaults中的数据,因为是按照key-value形式存储,所以循环key就能够删除数据

- (void)clearDefaults{
    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
    NSDictionary * dict = [defs dictionaryRepresentation];
    for (id key in dict)
    {
        [defs removeObjectForKey:key];
    }
    [defs synchronize];
}复制代码

GCD timer定时器的使用

这里的定时器,是一个每秒在主线程跑的一个方法

 __block int countSecond = 30; //倒计时
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(timer, ^{
        if (countSecond==0) { //倒计时完毕
            //@"倒计时结束,关闭"
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //倒计时完毕需要执行的操作
            });
        }else{ //倒计时
            NSLog(@"%@", [NSString stringWithFormat:@"%ld",(long)countSecond]);
            dispatch_async(dispatch_get_main_queue(), ^{
                //每秒需要执行的操作
                //在这里更新UI之类的
            });

            countSecond--;
        }
    });
    dispatch_resume(timer);复制代码

计算文件大小

- (long long)fileSizeAtPath:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:path])
    {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size;
    }

    return 0;
}复制代码

计算文件夹大小

- (long long)folderSizeAtPath:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    long long folderSize = 0;

    if ([fileManager fileExistsAtPath:path])
    {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles)
        {
            NSString *fileAbsolutePath = [path stringByAppendingPathComponent:fileName];
            if ([fileManager fileExistsAtPath:fileAbsolutePath])
            {
                long long size = [fileManager attributesOfItemAtPath:fileAbsolutePath error:nil].fileSize;
                folderSize += size;
            }
        }
    }

    return folderSize;
}复制代码

向上取整和向下取整

floor(x)函数,是一个向下取整函数,是一个C函数 即是去不大于x的一个最大整数
floor(3.12) = 3 floor(4.9) = 4

与floor(x)函数对应的是ceil函数
这个即是向上取整了
ceil(3.9) = 4  ceil(1.2) = 2复制代码

给任何一个view设置一张图片

UIImage *image = [UIImage imageNamed:@"image"];
self.MYView.layer.contents = (__bridge id _Nullable)(image.CGImage);
self.MYView.layer.contentsRect = CGRectMake(0, 0, 0.5, 0.5);复制代码

未完待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值