XHLaunchAdButton+RQExtension.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // XHLaunchAdButton+RQExtension.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2020/4/9.
  6. // Copyright © 2020 JCZ. All rights reserved.
  7. //
  8. #import "XHLaunchAdButton+RQExtension.h"
  9. #import <objc/runtime.h>
  10. /** Progress颜色 */
  11. #define RoundProgressColor [UIColor whiteColor]
  12. /** 背景色 */
  13. #define BackgroundColor [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]
  14. /** 字体颜色 */
  15. #define FontColor [UIColor whiteColor]
  16. #define SkipTitle @"跳过"
  17. /** 倒计时单位 */
  18. #define DurationUnit @""
  19. @interface XHLaunchAdButton (RQExtennsion)
  20. @property(nonatomic,strong)UILabel *timeLab;
  21. @end
  22. @implementation XHLaunchAdButton (RQExtension)
  23. + (void)load {
  24. // 方法交换,为的是当XHLaunchAdButton调用setTitleWithSkipType时候,调用的是我们的my_setTitleWithSkipType方法
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. Class class = [self class];
  28. SEL originalSelector = @selector(setTitleWithSkipType:duration:);
  29. SEL swizzledSelector = @selector(my_setTitleWithSkipType:duration:);
  30. Method originalMethod = class_getInstanceMethod(class, originalSelector);
  31. Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
  32. BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
  33. if (success) {
  34. class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  35. } else {
  36. method_exchangeImplementations(originalMethod, swizzledMethod);
  37. }
  38. });
  39. }
  40. - (void)my_setTitleWithSkipType:(SkipType)skipType duration:(NSInteger)duration{
  41. [self my_setTitleWithSkipType:skipType duration:duration]; // 由于方法交换,实际上调用的是XHLaunchAdButton的setTitleWithSkipType
  42. /// 禁止跳过
  43. self.userInteractionEnabled = NO;
  44. switch (skipType) {
  45. case SkipTypeNone:{
  46. self.hidden = YES;
  47. }
  48. break;
  49. case SkipTypeTime:{
  50. self.hidden = NO;
  51. self.timeLab.text = [NSString stringWithFormat:@"%ld %@",duration,DurationUnit];
  52. }
  53. break;
  54. case SkipTypeText:{
  55. self.hidden = NO;
  56. self.timeLab.text = SkipTitle;
  57. }
  58. break;
  59. case SkipTypeTimeText:{
  60. self.hidden = NO;
  61. self.timeLab.text = [NSString stringWithFormat:@"%ld %@",duration,SkipTitle];
  62. }
  63. break;
  64. case SkipTypeRoundTime:{
  65. self.hidden = NO;
  66. self.timeLab.text = [NSString stringWithFormat:@"%ld %@",duration,DurationUnit];
  67. }
  68. break;
  69. case SkipTypeRoundText:{
  70. self.hidden = NO;
  71. self.timeLab.text = SkipTitle;
  72. }
  73. break;
  74. case SkipTypeRoundProgressTime:{
  75. self.hidden = NO;
  76. self.timeLab.text = [NSString stringWithFormat:@"%ld %@",duration,DurationUnit];
  77. }
  78. break;
  79. case SkipTypeRoundProgressText:{
  80. self.hidden = NO;
  81. self.timeLab.text = SkipTitle;
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. @end