iOS基础开发技巧2

来源:互联网 发布:手机博彩程序源码 编辑:IT博客网 时间:2019/10/18 01:45

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

在UIView的扩展 快速修改frame

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

新建一个category UIView+PPSFrame.h

#import <UIKit/UIKit.h>
@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

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;

获取某个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);
0 0