// // XHLaunchAdButton+RQExtension.m // XinShouJiaDao // // Created by 张嵘 on 2020/4/9. // Copyright © 2020 JCZ. All rights reserved. // #import "XHLaunchAdButton+RQExtension.h" #import /** Progress颜色 */ #define RoundProgressColor [UIColor whiteColor] /** 背景色 */ #define BackgroundColor [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4] /** 字体颜色 */ #define FontColor [UIColor whiteColor] #define SkipTitle @"跳过" /** 倒计时单位 */ #define DurationUnit @"" @interface XHLaunchAdButton (RQExtennsion) @property(nonatomic,strong)UILabel *timeLab; @end @implementation XHLaunchAdButton (RQExtension) + (void)load { // 方法交换,为的是当XHLaunchAdButton调用setTitleWithSkipType时候,调用的是我们的my_setTitleWithSkipType方法 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class]; SEL originalSelector = @selector(setTitleWithSkipType:duration:); SEL swizzledSelector = @selector(my_setTitleWithSkipType:duration:); Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (success) { class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } }); } - (void)my_setTitleWithSkipType:(SkipType)skipType duration:(NSInteger)duration{ [self my_setTitleWithSkipType:skipType duration:duration]; // 由于方法交换,实际上调用的是XHLaunchAdButton的setTitleWithSkipType /// 禁止跳过 self.userInteractionEnabled = NO; switch (skipType) { case SkipTypeNone:{ self.hidden = YES; } break; case SkipTypeTime:{ self.hidden = NO; self.timeLab.text = [NSString stringWithFormat:@"%ld %@",(long)duration,DurationUnit]; } break; case SkipTypeText:{ self.hidden = NO; self.timeLab.text = SkipTitle; } break; case SkipTypeTimeText:{ self.hidden = NO; self.timeLab.text = [NSString stringWithFormat:@"%ld %@",(long)duration,SkipTitle]; } break; case SkipTypeRoundTime:{ self.hidden = NO; self.timeLab.text = [NSString stringWithFormat:@"%ld %@",(long)duration,DurationUnit]; } break; case SkipTypeRoundText:{ self.hidden = NO; self.timeLab.text = SkipTitle; } break; case SkipTypeRoundProgressTime:{ self.hidden = NO; self.timeLab.text = [NSString stringWithFormat:@"%ld %@",(long)duration,DurationUnit]; } break; case SkipTypeRoundProgressText:{ self.hidden = NO; self.timeLab.text = SkipTitle; } break; default: break; } } @end