《省钱达人》与《猎豆优选》UI相同版。域名tbk

UIButton+YHExtend.m 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // UIButton+YHExtend.m
  3. // YouHuiProject
  4. //
  5. // Created by jcymac on 2018/5/26.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "UIButton+YHExtend.h"
  9. @implementation UIButton (YHExtend)
  10. - (void)startWithTime:(NSInteger)timeLine title:(NSString *)title countDownTitle:(NSString *)subTitle mainColor:(UIColor *)mColor countColor:(UIColor *)color {
  11. //倒计时时间
  12. __block NSInteger timeOut = timeLine;
  13. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  14. dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  15. //每秒执行一次
  16. dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
  17. dispatch_source_set_event_handler(_timer, ^{
  18. //倒计时结束,关闭
  19. if (timeOut <= 0) {
  20. dispatch_source_cancel(_timer);
  21. dispatch_async(dispatch_get_main_queue(), ^{
  22. self.backgroundColor = mColor;
  23. [self setTitle:title forState:UIControlStateNormal];
  24. self.userInteractionEnabled = YES;
  25. });
  26. } else {
  27. int allTime = (int)timeLine + 1;
  28. int seconds = timeOut % allTime;
  29. NSString *timeStr = [NSString stringWithFormat:@"%0.2d", seconds];
  30. dispatch_async(dispatch_get_main_queue(), ^{
  31. self.backgroundColor = color;
  32. [self setTitle:[NSString stringWithFormat:@"%@%@",timeStr,subTitle] forState:UIControlStateNormal];
  33. self.userInteractionEnabled = NO;
  34. });
  35. timeOut--;
  36. }
  37. });
  38. dispatch_resume(_timer);
  39. }
  40. @end