IOS開發--常用工具類收集整理(Objective-C)(持續更新)


 前言:整理和收集了IOS項目開發常用的工具類,最后也給出了源碼下載鏈接。

這些可復用的工具,一定會給你實際項目開發工作錦上添花,會給你帶來大大的工作效率。

重復造輪子的事情,除卻自我多練習編碼之外,就不要傻傻的重復造輪子了,還是提高工作效率,早點完成工作早點回家陪老婆孩子。

所以下面備份的常用工具類一定是你需要的。

前提:你有一定的開發經驗,知道它們在開發的什么地方需要,你都不知道用在哪里,那你需要個毛啊,還是好好另外學好基礎吧。少兒不宜,請離開哦👻。

插件目錄列表:(持續更新和添加)

1、UIImage+RenderMode.h

2、UIColor+Hex.h

3、UIView+AdjustFrame.h(補充:Adjust是"調整"的意思)

4、宏定義定義app開發常用尺寸,例如屏幕寬高,iPhone4的尺寸等等

5、關於處理顏色比較好的工具類,功能:NSCache緩存處理顏色對象提高性能、十六進制顏色值處理(包含了UIColor+Hex.h的功能)等等

6、關於可以修改系統的導航控制器導航條顏色和透明度的工具類別

7、NSTimer+Addition 關於計時器的類別工具

8、給任意的UIView添加點擊事件

 

IOS工具類源碼github下載地址:https://github.com/HeYang123456789/Objective-C-Tools

覺得好,記得在我的github上點個贊,嘻嘻:

 

1、讓圖片不要渲染的工具類

簡介:

 

直接看這個工具類的源碼就知道,怎么設置了:

 1 //
 2 //  UIImage+RenderMode.h
 3 //  BaisiTest
 4 //
 5 //  Created by HEYANG on 16/1/19.
 6 //  Copyright © 2016年 HEYANG. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface UIImage (RenderMode)
12 
13 + (UIImage *)imageRenderingModeImageNamed:(NSString *)imageName;
14 
15 @end
16 
17 =================================================
18 
19 //
20 //  UIImage+RenderMode.m
21 //  BaisiTest
22 //
23 //  Created by HEYANG on 16/1/19.
24 //  Copyright © 2016年 HEYANG. All rights reserved.
25 //
26 
27 #import "UIImage+RenderMode.h"
28 
29 @implementation UIImage (RenderMode)
30 
31 + (UIImage *)imageRenderingModeImageNamed:(NSString *)imageName
32 {
33     UIImage *image = [UIImage imageNamed:imageName];
34     
35     return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
36 }
37 
38 @end
看源碼點擊坐標的"+"按鈕

使用代碼示例:

技術使用小小貼士:原本使用imageNamed會在KSImageNamed第三方插件自動彈出圖片搜索提示,但是換了這個imageRenderingModeImageNamed就不會有提示。

解決方案:

  方案1:就是上面圖示的,先用原生的imageNamed方法彈出搜索找到目標圖片文件,然后拷貝到這個imageRenderingModeImageNamed拓展方法使用。

  方案2:找到KSimageNamed的源碼,然后修改Completions.plist文件,然后再運行重新安裝這個KSImageNamed插件。

    

  方案3:

  • 百度到Xcode插件目錄是,~/Library/Application Support/Developer/Shared/Xcode/Plug-ins,但是根據我的探索經驗得到的結果是,有的XCode插件是直接安裝到這個目錄下的,而KSImageNamed插件不是在這個地方的。
  • KSImageNamed插件在該路徑下/Users/HeYang/Library/Developer/Xcode/Plug-ins(補充:這是我的路徑,HeYang是我的電腦用戶名,如果你自己的電腦就替換成你的用戶名嘍,這點智商不會為難你吧)。當然,這個目錄從Library開始是默認隱藏的,最快的解決方法是,按commad+shift+G,然后把/Users/你電腦用戶名/Library/Developer/Xcode/Plug-ins輸入進去,

    

    回車就能打開目標目錄,然后你就會看到:

    

    找到KSImageNamed插件,然后打開插件包內容,找到同樣的Completions.plist文件,和方案2一樣進行更改。然后直接重新啟動XCode。

    然后使用效果:

    

 

2、提供十六進制轉為色值的方法。

簡介:項目實際開發中,美工提供給我們的顏色值可能是#ffffff十六進制或者是OC上的表示形式0Xffffff,而不是直接的RGB色值,

但是UIKit沒有提供直接處理這兩種情況的顏色值的方法,所以就需要我們自己對UIColor進行類別拓展方法:

 

源碼:

 1 //
 2 //  UIColor+Hex.h
 3 //  Hello
 4 //
 5 //  Created by HEYANG on 16/1/20.
 6 //  Copyright © 2016年 HEYANG. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface UIColor (Hex)
12 
13 /** 默認alpha為1 */
14 + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;
15 
16 /** 從十六進制字符串獲取顏色,默認alpha為1 */
17 + (UIColor *)colorWithHexString:(NSString *)color;
18 
19 /** 從十六進制字符串獲取顏色,alpha需要自己傳遞 color:支持@“#123456”、 @“0X123456”、 @“123456”三種格式 */
20 + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;
21 
22 
23 @end
24 
25 =================================================
26 
27 //
28 //  UIColor+Hex.m
29 //  Hello
30 //
31 //  Created by HEYANG on 16/1/20.
32 //  Copyright © 2016年 HEYANG. All rights reserved.
33 //
34 
35 #import "UIColor+Hex.h"
36 
37 @implementation UIColor (Hex)
38 
39 + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue{
40     return [UIColor colorWithRed:red green:green blue:blue alpha:1];
41 }
42 
43 + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
44 {
45     //刪除字符串中的空格
46     NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
47     // String should be 6 or 8 characters
48     if ([cString length] < 6)
49     {
50         return [UIColor clearColor];
51     }
52     // strip 0X if it appears
53     //如果是0x開頭的,那么截取字符串,字符串從索引為2的位置開始,一直到末尾
54     if ([cString hasPrefix:@"0X"])
55     {
56         cString = [cString substringFromIndex:2];
57     }
58     //如果是#開頭的,那么截取字符串,字符串從索引為1的位置開始,一直到末尾
59     if ([cString hasPrefix:@"#"])
60     {
61         cString = [cString substringFromIndex:1];
62     }
63     if ([cString length] != 6)
64     {
65         return [UIColor clearColor];
66     }
67     
68     // Separate into r, g, b substrings
69     NSRange range;
70     range.location = 0;
71     range.length = 2;
72     //r                       截取的range = (0,2)
73     NSString *rString = [cString substringWithRange:range];
74     //g
75     range.location = 2;//     截取的range = (2,2)
76     NSString *gString = [cString substringWithRange:range];
77     //b
78     range.location = 4;//     截取的range = (4,2)
79     NSString *bString = [cString substringWithRange:range];
80     
81     // Scan values
82     unsigned int r, g, b;//將字符串十六進制兩位數字轉為十進制整數
83     [[NSScanner scannerWithString:rString] scanHexInt:&r];
84     [[NSScanner scannerWithString:gString] scanHexInt:&g];
85     [[NSScanner scannerWithString:bString] scanHexInt:&b];
86     return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
87 }
88 
89 //默認alpha值為1
90 + (UIColor *)colorWithHexString:(NSString *)color
91 {
92     return [self colorWithHexString:color alpha:1.0f];
93 }
94 
95 @end
點擊坐標的"+"按鈕就能打開源碼

使用代碼示例:

 

 

3、直接調整UIView的Frame的x,y,width,height,origin,size值

簡介:關於UIView的Frame的x,y,width,height,origin,size值,在Objective-C中是無法直接賦值修改(在Swift中是可以直接賦值修改的),只能間接賦值修改,所以就需要將這部分抽離成

一個可復用的分類。補充:為了避免以后重復利用的過程中,方法名會和項目中其他代碼方法名沖突,所以在屬性和方法前面加了語義明確的前綴"adjust_",英文adjust就是調整的意思。

源碼:

  1 //
  2 //  UIView+AdjustFrame.h   Adjust:調整
  3 //  Hello
  4 //
  5 //  Created by HEYANG on 16/1/20.
  6 //  Copyright © 2016年 HEYANG. All rights reserved.
  7 //
  8 
  9 #import <UIKit/UIKit.h>
 10 
 11 @interface UIView (AdjustFrame)
 12 
 13 //類別可以拓展屬性,但是不能生成set和get方法
 14 @property (assign, nonatomic) CGFloat adjust_x;
 15 @property (assign, nonatomic) CGFloat adjust_y;
 16 @property (assign, nonatomic) CGFloat adjust_width;
 17 @property (assign, nonatomic) CGFloat adjust_height;
 18 @property (assign, nonatomic) CGSize adjust_size;
 19 @property (assign, nonatomic) CGPoint adjust_origin;
 20 
 21 @end
 22 
 23 =================================================
 24 
 25 //
 26 //  UIView+AdjustFrame.m  Adjust:調整
 27 //  Hello
 28 //
 29 //  Created by HEYANG on 16/1/20.
 30 //  Copyright © 2016年 HEYANG. All rights reserved.
 31 //
 32 
 33 #import "UIView+AdjustFrame.h"
 34 
 35 @implementation UIView (AdjustFrame)
 36 
 37 #pragma mark - adjust_x
 38 -(void)setAdjust_x:(CGFloat)adjust_x{
 39     CGRect frame = self.frame;
 40     frame.origin.x = adjust_x;
 41     self.frame = frame;
 42 }
 43 
 44 -(CGFloat)adjust_x{
 45     return self.frame.origin.x;
 46 }
 47 
 48 #pragma mark - adjust_y
 49 -(void)setAdjust_y:(CGFloat)adjust_y{
 50     CGRect frame = self.frame;
 51     frame.origin.y = adjust_y;
 52     self.frame = frame;
 53 }
 54 
 55 - (CGFloat)adjust_y
 56 {
 57     return self.frame.origin.y;
 58 }
 59 
 60 #pragma mark - adjust_width
 61 -(void)setAdjust_width:(CGFloat)adjust_width{
 62     CGRect frame = self.frame;
 63     frame.size.width = adjust_width;
 64     self.frame = frame;
 65 }
 66 - (CGFloat)adjust_width
 67 {
 68     return self.frame.size.width;
 69 }
 70 
 71 #pragma mark - adjust_height
 72 -(void)setAdjust_height:(CGFloat)adjust_height{
 73     CGRect frame = self.frame;
 74     frame.size.height = adjust_height;
 75     self.frame = frame;
 76 }
 77 - (CGFloat)adjust_height
 78 {
 79     return self.frame.size.height;
 80 }
 81 
 82 #pragma mark - adjust_size
 83 -(void)setAdjust_size:(CGSize)adjust_size{
 84     CGRect frame = self.frame;
 85     frame.size = adjust_size;
 86     self.frame = frame;
 87 }
 88 - (CGSize)adjust_size
 89 {
 90     return self.frame.size;
 91 }
 92 
 93 #pragma mark - adjust_origin
 94 -(void)setAdjust_origin:(CGPoint)adjust_origin{
 95     CGRect frame = self.frame;
 96     frame.origin = adjust_origin;
 97     self.frame = frame;
 98 }
 99 - (CGPoint)adjust_origin
100 {
101     return self.frame.origin;
102 }
103 
104 @end
點擊左邊的"+"按鈕就能打開看到源碼

代碼使用示例:

 4、宏定義定義app開發常用尺寸,例如屏幕寬高,iPhone4的尺寸等等

源碼來自YouXianMing老師的親筆:

 1 //
 2 //  WxHxD.h
 3 //
 4 //  Created by YouXianMing on 14/10/29.
 5 //  Copyright (c) 2014年 YouXianMing. All rights reserved.
 6 //
 7 
 8 // 寬度
 9 #define  UIScreenWidth                     [UIScreen mainScreen].bounds.size.width
10 
11 // 高度
12 #define  UIScreenHeight                    [UIScreen mainScreen].bounds.size.height
13 
14 // 狀態欄高度
15 #define  StatusBarHeight                   20.f
16 
17 // 導航欄高度
18 #define  NavigationBarHeight               44.f
19 
20 // 標簽欄高度
21 #define  TabbarHeight                      49.f
22 
23 // 狀態欄高度 + 導航欄高度
24 #define  StatusBarAndNavigationBarHeight   (20.f + 44.f)
25 
26 #define  iPhone4_4s   (Width == 320.f && Height == 480.f ? YES : NO)
27 #define  iPhone5_5s   (Width == 320.f && Height == 568.f ? YES : NO)
28 #define  iPhone6      (Width == 375.f && Height == 667.f ? YES : NO)
29 #define  iPhone6_plus (Width == 414.f && Height == 736.f ? YES : NO)//? YES : NO 這么寫增加可讀性

改進:

1 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
2 
3 #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
4 
5 #define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
6 
7 #define iPhone6p ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1472), [[UIScreen mainScreen] currentMode].size) : NO)

 

5、關於處理顏色比較好的工具類,功能:NSCache緩存處理顏色對象提高性能、十六進制顏色值處理(包含了UIColor+Hex.h的功能)等等 

github源碼:https://github.com/HeYang123456789/Tools-Test-Project/tree/master/ColorStringTool/ColorStringTool/ColorStringTool

使用實例:

 1 #import "ViewController.h"
 2 #import "ColorTools.h"
 3 
 4 
 5 @interface ViewController ()
 6 
 7 @property (weak, nonatomic) IBOutlet UIView *colorView;
 8 @property (weak, nonatomic) IBOutlet UIView *colorView2;
 9 
10 @end
11 
12 @implementation ViewController
13 
14 - (void)viewDidLoad {
15 
16     //通過ColorTools內部的webColor的key獲取顏色值,並會放入緩存中
17     self.colorView.layer.borderColor = [UIColor colorWithHexString:@"black"].CGColor;
18     self.colorView.layer.borderWidth = 2.0f;
19     self.colorView.layer.cornerRadius = 4.0f;
20 
21     //通過UIColor的類方法獲取UIColor對象
22     NSString* colorStr = @"0xff0fff";
23     UIColor* color = [UIColor colorWithHexString:colorStr];
24     self.colorView.backgroundColor = color;
25 
26     //通過@"0xff0fff"本身獲取UIColor對象
27     UIColor* color2 = [@"#ae2388ff" getColorFromColorhexa];
28     self.colorView2.backgroundColor = color2;
29 
30     //宏定義的debug模式的打印兩個顏色對象,看看是不是具體實例
31     LogColor(color)
32     LogColor(color2)
33 }
34 
35 @end

運行效果和打印結果:

 

6、關於可以修改系統的導航控制器導航條顏色和透明度的工具類別

github網址:https://github.com/HeYang123456789/NavigationBarResetBackgroundDemo

效果展示:

另外呢,關於這個導航條顏色的設計,可以學習本人的《IOS之UI--動態設置NavigationBar的顏色以及透明度

 

7、關於計時器的工具類別

備份百度雲下載鏈接: http://pan.baidu.com/s/1bXjFNg 密碼: zpkj

如果想對NSTimer進一步學習,可以閱讀本人的筆記:《NSTimer定時器的使用

 

 

8、給任意的UIView添加點擊事件

這個工具類被本人寫在另一篇隨筆中了:《給任意的UIView添加點擊事件

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM