CustomLabelButton.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // CustomLabelButton.m
  3. // OneCable
  4. //
  5. // Created by Jason Lee on 1/7/16.
  6. // Copyright © 2016 ntels. All rights reserved.
  7. //
  8. #import "CustomLabelButton.h"
  9. #import "UIButton+WebCache.h"
  10. #import "CustomButton.h"
  11. #import "CustomLabel.h"
  12. @implementation CustomLabelButton
  13. - (void)didMoveToSuperview {
  14. if (!self.superview)
  15. return;
  16. if (!_label) {
  17. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  18. [self didMoveToSuperview];
  19. });
  20. return;
  21. }
  22. [_label addTouchEventHandler:^(id label) {
  23. [self labelTouched:label];
  24. }];
  25. }
  26. - (void)setTitle:(NSString *)title {
  27. _label.text = title;
  28. }
  29. - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state {
  30. [_btn sd_setImageWithURL:url forState:state placeholderImage:nil options:SDWebImageRefreshCached];
  31. }
  32. - (void)setEnabled:(BOOL)enabled {
  33. _btn.enabled = enabled;
  34. if (enabled) {
  35. if (!_label.touchHandler) {
  36. [_label addTouchEventHandler:^(id label) {
  37. [self labelTouched:label];
  38. }];
  39. }
  40. } else {
  41. _label.touchHandler = nil;
  42. }
  43. }
  44. #pragma mark - UIEvents
  45. - (void)labelTouched:(id)sender {
  46. [_btn setHighlighted:YES];
  47. [_btn sendActionsForControlEvents:UIControlEventTouchUpInside];
  48. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kfAnimationDur * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  49. [_btn setHighlighted:NO];
  50. });
  51. }
  52. - (nullable NSArray<NSString *> *)actionsForTarget:(nullable id)target forControlEvent:(UIControlEvents)controlEvent {
  53. return [_btn actionsForTarget:target forControlEvent:controlEvent];
  54. }
  55. - (void)addTarget:(nullable id)target action:(nonnull SEL)action forControlEvents:(UIControlEvents)controlEvents {
  56. [_btn addTarget:target action:action forControlEvents:controlEvents];
  57. [_label addTouchEventHandler:^(id label) {
  58. [target performSelector:action withObject:nil];
  59. }];
  60. }
  61. @end