美文网首页
iOS编程常见的工具类

iOS编程常见的工具类

作者: 单线程Jack | 来源:发表于2019-07-15 16:39 被阅读0次

1、UIImage+RenderMode.h

#import <UIKit/UIKit.h>

@interface UIImage (RenderMode)

/** 取消UIImage的渲染模式 */
+ (UIImage *)imageRenderingModeImageNamed:(NSString *)imageName;

@end
#import "UIImage+RenderMode.h"

@implementation UIImage (RenderMode)

+ (UIImage *)imageRenderingModeImageNamed:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

@end

2、UIColor+Hex.h

#import <UIKit/UIKit.h>

@interface UIColor (Hex)

/** 默认alpha为1 */
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;

/** 从十六进制字符串获取颜色,默认alpha为1 */
+ (UIColor *)colorWithHexString:(NSString *)color;

/** 从十六进制字符串获取颜色,alpha需要自己传递 color:支持@“#123456”、 @“0X123456”、 @“123456”三种格式 */
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;

@end
#import "UIColor+Hex.h"

@implementation UIColor (Hex)

+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue{
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r                       截取的range = (0,2)
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;//     截取的range = (2,2)
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;//     截取的range = (4,2)
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;//将字符串十六进制两位数字转为十进制整数
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

//默认alpha值为1
+ (UIColor *)colorWithHexString:(NSString *)color
{
    return [self colorWithHexString:color alpha:1.0f];
}

@end

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

#import <UIKit/UIKit.h>

@interface UIView (AdjustFrame)

//类别可以拓展属性,但是不能生成set和get方法
@property (assign, nonatomic) CGFloat adjust_x;
@property (assign, nonatomic) CGFloat adjust_y;
@property (assign, nonatomic) CGFloat adjust_width;
@property (assign, nonatomic) CGFloat adjust_height;
@property (assign, nonatomic) CGSize adjust_size;
@property (assign, nonatomic) CGPoint adjust_origin;

@end
#import "UIView+AdjustFrame.h"

@implementation UIView (AdjustFrame)

#pragma mark - adjust_x
-(void)setAdjust_x:(CGFloat)adjust_x{
    CGRect frame = self.frame;
    frame.origin.x = adjust_x;
    self.frame = frame;
}

-(CGFloat)adjust_x{
    return self.frame.origin.x;
}

#pragma mark - adjust_y
-(void)setAdjust_y:(CGFloat)adjust_y{
    CGRect frame = self.frame;
    frame.origin.y = adjust_y;
    self.frame = frame;
}

- (CGFloat)adjust_y
{
    return self.frame.origin.y;
}

#pragma mark - adjust_width
-(void)setAdjust_width:(CGFloat)adjust_width{
    CGRect frame = self.frame;
    frame.size.width = adjust_width;
    self.frame = frame;
}
- (CGFloat)adjust_width
{
    return self.frame.size.width;
}

#pragma mark - adjust_height
-(void)setAdjust_height:(CGFloat)adjust_height{
    CGRect frame = self.frame;
    frame.size.height = adjust_height;
    self.frame = frame;
}
- (CGFloat)adjust_height
{
    return self.frame.size.height;
}

#pragma mark - adjust_size
-(void)setAdjust_size:(CGSize)adjust_size{
    CGRect frame = self.frame;
    frame.size = adjust_size;
    self.frame = frame;
}
- (CGSize)adjust_size
{
    return self.frame.size;
}

#pragma mark - adjust_origin
-(void)setAdjust_origin:(CGPoint)adjust_origin{
    CGRect frame = self.frame;
    frame.origin = adjust_origin;
    self.frame = frame;
}
- (CGPoint)adjust_origin
{
    return self.frame.origin;
}

@end

4、关于处理颜色比较好的工具类,功能:NSCache缓存处理颜色对象提高性能、十六进制颜色值处理(包含了UIColor+Hex.h的功能)等等

#import <UIKit/UIKit.h>

@interface UIColor (Hex)

/** 默认alpha为1 */
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;

/** 从十六进制字符串获取颜色,默认alpha为1 */
+ (UIColor *)colorWithHexString:(NSString *)color;

/** 从十六进制字符串获取颜色,alpha需要自己传递 color:支持@“#123456”、 @“0X123456”、 @“123456”三种格式 */
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;


@end
#import "UIColor+Hex.h"

@implementation UIColor (Hex)

+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue{
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r                       截取的range = (0,2)
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;//     截取的range = (2,2)
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;//     截取的range = (4,2)
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;//将字符串十六进制两位数字转为十进制整数
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

//默认alpha值为1
+ (UIColor *)colorWithHexString:(NSString *)color
{
    return [self colorWithHexString:color alpha:1.0f];
}

@end

5、NSTimer+Addition 关于计时器的类别工具

#import <Foundation/Foundation.h>

@interface NSTimer (Addition)

- (void)pauseTimer;
- (void)resumeTimer;
- (void)resumeTimerAfterTimeInterval:(NSTimeInterval)interval;
@end
#import "NSTimer+Addition.h"

@implementation NSTimer (Addition)

-(void)pauseTimer
{
    if (![self isValid]) {
        return ;
    }
    [self setFireDate:[NSDate distantFuture]];
}


-(void)resumeTimer
{
    if (![self isValid]) {
        return ;
    }
    [self setFireDate:[NSDate date]];
}

- (void)resumeTimerAfterTimeInterval:(NSTimeInterval)interval
{
    if (![self isValid]) {
        return ;
    }
    [self setFireDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
}

@end

6、给任意的UIView添加点击事件

 #import <UIKit/UIKit.h>
 
 @interface UIView (AddClickedEvent)
 
 - (void)addClickedBlock:(void(^)(id obj))tapAction;
 
 @end
#import "UIView+AddClickedEvent.h"
 #import <objc/message.h>

 @interface UIView ()
 
 @property void(^clickedAction)(id);
 
 @end
  
 @implementation UIView (AddClickedEvent)
 
 - (void)setClickedAction:(void (^)(id))clickedAction{
     objc_setAssociatedObject(self, @"AddClickedEvent", clickedAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(id))clickedAction{
     return objc_getAssociatedObject(self, @"AddClickedEvent");
 }
 
 - (void)addClickedBlock:(void(^)(id obj))clickedAction{
     self.clickedAction = clickedAction;
     // hy:先判断当前是否有交互事件,如果没有的话。。。所有gesture的交互事件都会被添加进gestureRecognizers中
     if (![self gestureRecognizers]) {
        self.userInteractionEnabled = YES;
         // hy:添加单击事件
         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
         [self addGestureRecognizer:tap];
     }
 }
 
- (void)tap{
     if (self.clickedAction) {
         self.clickedAction(self);
     }
 }
  @end

相关文章

网友评论

      本文标题:iOS编程常见的工具类

      本文链接:https://www.haomeiwen.com/subject/csonkctx.html