iOS基礎開發技巧2

這裏主要講一些我在日常開發中用到的一些小的技巧,其實也算不上技巧吧,就是省去一些不必要的代碼,或者有的小問題困擾你很久說不行在這裏你能找到答案

在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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章