1.IOS常用工具类收集整理

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">

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 这么写增加可读性

</pre>

[[图片上传中...(image-6c4f3-1509751967770-1)]](javascript:void(0); "复制代码")

改进:

[[图片上传中...(image-3265d6-1509751967770-0)]](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">

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)</pre>

1. 前言

整理和收集了IOS项目开发常用的工具类,最后也给出了源码下载链接。
这些可复用的工具,一定会给你实际项目开发工作锦上添花,会给你带来大大的工作效率。
重复造轮子的事情,除却自我多练习编码之外,就不要傻傻的重复造轮子了,
还是提高工作效率,早点完成工作早点回家陪老婆孩子。

2.插件目录列表:

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

2.1让图片不要渲染的工具类

简介:


image.png

使用代码实例:


image.png

2.2提供十六进制转为色值的方法

开发背景:
项目实际开发中,美工提供给我们的颜色值可能是#ffffff十六进制或者是OC上的表示形式0Xffffff,而不是直接的RGB色值,
但是UIKit没有提供直接处理这两种情况的颜色值的方法,所以就需要我们自己对UIColor进行类别拓展方法:
image.png

代码:

//
 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

2.3直接调整UIView的Frame的x,y,width,height,origin,size值

简介:关于UIView的Frame的x,y,width,height,origin,size值,在Objective-C中是无法直接赋值修改(在Swift中是可以直接赋值修改的),只能间接赋值修改,所以就需要将这部分抽离成
一个可复用的分类。补充:为了避免以后重复利用的过程中,方法名会和项目中其他代码方法名冲突,所以在属性和方法前面加了语义明确的前缀"adjust_",英文adjust就是调整的意思。

源码:

//
  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

2.4.宏定义app开发常用的尺寸,例如屏幕宽高,iphone4的等等

image.png

宏定义的源码:

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 这么写增加可读性

2.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

2.6修改系统的导航栏控制器的导航栏颜色和透明度的工具类别:

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

2.7关于计时器的工具类别:

2.8给任意的UIView添加点击事件

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容