UIPageControl+LhGray.m 1.48 KB
//
//  UIPageControl+LhGray.m
//  SmartCity
//
//  Created by luoh on 2021/3/25.
//  Copyright © 2021 StarHome. All rights reserved.
//

#import "UIPageControl+LhGray.h"
#import <objc/runtime.h>

@implementation UIPageControl (LhGray)

+(void)lh_pageControlSwizzldMethedWith:(BOOL)changeGray{
    if (changeGray == false) {
        return;
    }
    Class cls = [self class];
    
    SEL oriSel = @selector(setCurrentPageIndicatorTintColor:);
    SEL swizzldSel = @selector(lh_setCurrentPageIndicatorTintColor:);
    
    Method originMethed = class_getInstanceMethod(cls, oriSel);
    Method swizzldMethed = class_getInstanceMethod(cls, swizzldSel);
    
    BOOL isAddMethed = class_addMethod(cls, oriSel, method_getImplementation(swizzldMethed),method_getTypeEncoding(swizzldMethed));
    if (isAddMethed) {
        class_replaceMethod(cls, swizzldSel, method_getImplementation(originMethed), method_getTypeEncoding(originMethed));
    }else{
        method_exchangeImplementations(originMethed,swizzldMethed);
    }
}
-(void)lh_setCurrentPageIndicatorTintColor:(UIColor *)color{
    UIColor * newColor = [self changeGrayWithColor:color Red:1.0 green:0.0 blue:0.0 alpha:1.0];
    [self lh_setCurrentPageIndicatorTintColor:newColor];
    
}

- (UIColor *)changeGrayWithColor:(UIColor *)color Red:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a {
    CGFloat gray = r * 0.299 +g * 0.587 + b * 0.114;
    UIColor *grayColor = [UIColor colorWithWhite:gray alpha:a];
    return  grayColor;
}

@end