CNavigationRightView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // CNavigationRightView.m
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CNavigationRightView.h"
  13. #pragma mark - CNavigationRightAction
  14. @interface CNavigationRightAction ()
  15. @property(nonatomic, strong) UIImage *image;
  16. @property(nonatomic, assign) NSUInteger tag;
  17. @end
  18. @implementation CNavigationRightAction
  19. + (CNavigationRightAction *)actionWithImage:(UIImage *)image tag:(NSUInteger)tag {
  20. CNavigationRightAction *action = [[CNavigationRightAction alloc] init];
  21. action.image = image;
  22. action.tag = tag;
  23. return action;
  24. }
  25. @end
  26. #pragma mark - CNavigationRightView
  27. @interface CNavigationRightView ()
  28. typedef void (^Clickback) (NSUInteger tag);
  29. @property(nonatomic,copy)Clickback clickBack;
  30. @property(nonatomic,strong)NSArray <CNavigationRightAction *> *dataArray;
  31. @end
  32. @implementation CNavigationRightView
  33. #pragma mark - Initializers
  34. - (instancetype)initWithRightActions:(NSArray<CNavigationRightAction *> *)rightActions clickBack:(void (^)(NSUInteger tag))clickBack {
  35. if(self = [super init]) {
  36. self.dataArray = rightActions;
  37. [self configurationUI];
  38. self.clickBack = clickBack;
  39. }
  40. return self;
  41. }
  42. - (instancetype)initWithDefaultItemsClickBack:(void (^)(NSUInteger tag))clickBack {
  43. if(self = [super init]) {
  44. NSArray *nums = [self defaultItems];
  45. NSMutableArray *actions = [NSMutableArray array];
  46. for (NSNumber *num in nums) {
  47. NSString *imageName = nil;
  48. switch (num.integerValue) {
  49. case CNavigationRightTypeSearch:
  50. imageName = @"CNavigationImageNameSearch";
  51. break;
  52. case CNavigationRightTypeBota:
  53. imageName = @"CNavigationImageNameBota";
  54. break;
  55. default:
  56. case CNavigationRightTypeMore:
  57. imageName = @"CNavigationImageNameMore";
  58. break;
  59. }
  60. UIImage *image = [UIImage imageNamed:imageName
  61. inBundle:[NSBundle bundleForClass:self.class]
  62. compatibleWithTraitCollection:nil];
  63. CNavigationRightAction *action = [CNavigationRightAction actionWithImage:image tag:num.integerValue];
  64. [actions addObject:action];
  65. }
  66. self.dataArray = actions;
  67. [self configurationUI];
  68. self.clickBack = clickBack;
  69. }
  70. return self;
  71. }
  72. - (NSArray *)defaultItems {
  73. return @[@(CNavigationRightTypeSearch),@(CNavigationRightTypeBota),@(CNavigationRightTypeMore)];
  74. }
  75. #pragma mark - Private method
  76. - (void)configurationUI {
  77. CGFloat offset = 20;
  78. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  79. offset = 20;
  80. }
  81. CGFloat height = 0;
  82. CGFloat width = offset;
  83. for (NSUInteger i = 0; i< self.dataArray.count; i++) {
  84. CNavigationRightAction *rightAction = self.dataArray[i];
  85. UIImage *image = rightAction.image;
  86. height = MAX(height, image.size.height);
  87. if(i == 0) {
  88. width = 0;
  89. }
  90. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(width, 0, image.size.width, image.size.height)];
  91. [button setImage:image forState:UIControlStateNormal];
  92. button.tag = rightAction.tag;
  93. [button addTarget:self action:@selector(buttonClickItem:) forControlEvents:UIControlEventTouchUpInside];
  94. [self addSubview:button];
  95. if(i == self.dataArray.count - 1){
  96. width += 0;
  97. }else{
  98. width += (button.frame.size.width + offset);
  99. }
  100. }
  101. self.bounds = CGRectMake(0, 0, width + offset, height);
  102. }
  103. #pragma mark - Action
  104. - (IBAction)buttonClickItem:(UIButton *)sender {
  105. NSUInteger tag = sender.tag;
  106. if (self.clickBack) {
  107. self.clickBack(tag);
  108. }
  109. }
  110. @end