iOS 基礎開發技巧 (一)

歡迎你們關注個人公衆號,我會按期分享一些我在項目中遇到問題的解決辦法和一些iOS實用的技巧,現階段主要是整理出一些基礎的知識記錄下來
app

文章也會同步更新到個人博客:
ppsheep.comasync

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

iOS OC項目的pch文件使用

在項目中若是咱們須要一些公共的引用,或者一些全局的宏 那咱們常常在pch中設置好ui

具體怎麼設置呢 在項目下新建一個pch文件 atom

1

通常我會取名 項目名-PrefixHeaderspa

在target——>Bulid Setting 中 設置 PrefixHeader
線程

2

個人項目文件夾結構3d

$(SRCROOT)這個是指工程的根目錄code

找到這個pch文件就行 而後啓動APP就會編譯這個文件了orm

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);複製代碼

未完待續。。。

相關文章
相關標籤/搜索