KSChoosePicker.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // KSChoosePicker.m
  3. // TeacherDaya
  4. //
  5. // Created by Kyle on 2019/10/16.
  6. // Copyright © 2019 DayaMusic. All rights reserved.
  7. //
  8. #import "KSChoosePicker.h"
  9. @interface KSChoosePicker ()<UIPickerViewDelegate,UIPickerViewDataSource,UIGestureRecognizerDelegate>
  10. @property (nonatomic, strong) UIView *backView; // 背景图
  11. @property (nonatomic, strong) UIView *displayView;
  12. @property (nonatomic, strong) UIView *contentView;
  13. @property (nonatomic, strong) NSArray *sourceData;
  14. @property (nonatomic, strong) UIPickerView *picker; // 选择器
  15. @property (nonatomic, copy) PickerChooseCallback callback;
  16. @property (nonatomic, copy) PickerCancelCallback cancelCallback;
  17. @property (nonatomic, assign) NSInteger chooseIndex;
  18. @end
  19. @implementation KSChoosePicker
  20. - (instancetype)initWithTitle:(NSString * __nullable)title sourceData:(NSArray *)sourceData chooseReturnWithBlock:(PickerChooseCallback)callback cancel:(PickerCancelCallback)cancelCallback {
  21. if (self = [super init]) {
  22. if (callback) {
  23. self.callback = callback;
  24. }
  25. if (cancelCallback) {
  26. self.cancelCallback = cancelCallback;
  27. }
  28. self.sourceData = sourceData;
  29. self.frame = CGRectMake(0, 0, kScreen_Width, kScreen_Height);
  30. self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height)];
  31. self.backView.backgroundColor = HexRGBAlpha(0x000000, 0.5f);
  32. [self addSubview:_backView];
  33. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
  34. tapGesture.delegate = self;
  35. [self.backView addGestureRecognizer:tapGesture];
  36. self.displayView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreen_Height, kScreen_Width, 255)];
  37. self.displayView.backgroundColor = [UIColor whiteColor];
  38. [self.backView addSubview:self.displayView];
  39. self.displayView.layer.masksToBounds = YES;
  40. if (@available(iOS 11.0, *)) {
  41. self.displayView.layer.cornerRadius = 10;
  42. self.displayView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner; // 左上圆角
  43. }
  44. else {
  45. UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.displayView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
  46. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  47. maskLayer.frame = self.displayView.bounds;
  48. maskLayer.path = path.CGPath;
  49. self.displayView.layer.mask = maskLayer;
  50. }
  51. _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, kScreen_Width, 216)];
  52. _contentView.backgroundColor = [UIColor whiteColor];
  53. [_displayView addSubview:_contentView];
  54. [self initPickerViews];
  55. self.chooseIndex = 0;
  56. // 按钮
  57. UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
  58. [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
  59. cancleButton.titleLabel.font = [UIFont systemFontOfSize:17.0f];
  60. [cancleButton setTitleColor:HexRGB(0x666666) forState:UIControlStateNormal];
  61. [cancleButton addTarget:self action:@selector(cancleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  62. cancleButton.frame = CGRectMake(20, 10, 60, 30);
  63. [self.displayView addSubview:cancleButton];
  64. UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
  65. [sureButton setTitle:@"确定" forState:UIControlStateNormal];
  66. sureButton.titleLabel.font = [UIFont systemFontOfSize:17.0f];
  67. [sureButton setTitleColor:THEMECOLOR forState:UIControlStateNormal];
  68. [sureButton addTarget:self action:@selector(doneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  69. sureButton.frame = CGRectMake(kScreen_Width - 80, 10, 60, 30);
  70. [self.displayView addSubview:sureButton];
  71. [UIView animateWithDuration:0.3f animations:^{
  72. self.displayView.frame = CGRectMake(0, kScreen_Height - 255, kScreen_Width, 255);
  73. } completion:^(BOOL finished) {
  74. }];
  75. // titleLabel
  76. if (![NSString isEmptyString:title]) {
  77. UILabel *titleLabel = [[UILabel alloc] init];
  78. [titleLabel setTextColor:HexRGB(0x444444)];
  79. [titleLabel setFont:[UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium]];
  80. titleLabel.textAlignment = NSTextAlignmentCenter;
  81. titleLabel.text = title;
  82. [self.displayView addSubview:titleLabel];
  83. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.left.mas_equalTo(cancleButton.mas_right).offset(10);
  85. make.top.mas_equalTo(self.displayView.mas_top).offset(10);
  86. make.right.mas_equalTo(sureButton.mas_left).offset(-10);
  87. make.height.mas_equalTo(30);
  88. }];
  89. }
  90. }
  91. return self;
  92. }
  93. // 创建选择器
  94. - (void)initPickerViews {
  95. // 更新数据源
  96. CGRect frame = _contentView.frame;
  97. CGFloat height = CGRectGetHeight(frame);
  98. CGFloat width = CGRectGetWidth(frame);
  99. _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
  100. _picker.delegate = self;
  101. [_picker setShowsSelectionIndicator:YES];
  102. [_contentView addSubview:_picker];
  103. }
  104. - (void)cancleButtonAction:(UIButton *)sender {
  105. if (self.cancelCallback) {
  106. self.cancelCallback();
  107. }
  108. [UIView animateWithDuration:0.3f animations:^{
  109. self.displayView.frame = CGRectMake(0, kScreen_Height, kScreen_Width, 255);
  110. } completion:^(BOOL finished) {
  111. [self removeFromSuperview];
  112. }];
  113. }
  114. - (void)doneButtonAction:(UIButton *)sender {
  115. if (self.callback) {
  116. NSString *returnValue = self.sourceData[_chooseIndex];
  117. self.callback(returnValue, self.chooseIndex);
  118. }
  119. [UIView animateWithDuration:0.3f animations:^{
  120. self.displayView.frame = CGRectMake(0, kScreen_Height, kScreen_Width, 255);
  121. } completion:^(BOOL finished) {
  122. [self removeFromSuperview];
  123. }];
  124. }
  125. - (void)tapAction:(UITapGestureRecognizer *)gesture {
  126. if (self.cancelCallback) {
  127. self.cancelCallback();
  128. }
  129. [UIView animateWithDuration:0.3f animations:^{
  130. self.displayView.frame = CGRectMake(0, kScreen_Height, kScreen_Width, 255);
  131. } completion:^(BOOL finished) {
  132. [self removeFromSuperview];
  133. }];
  134. }
  135. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  136. if ([touch.view isDescendantOfView:self.displayView]) {
  137. return NO;
  138. }
  139. return YES;
  140. }
  141. - (void)showPicker {
  142. UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
  143. [window addSubview:self];
  144. }
  145. #pragma mark --- UIPickerView Datasource
  146. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  147. return 1;
  148. }
  149. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  150. return self.sourceData.count;
  151. }
  152. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  153. NSInteger index = row;
  154. NSString *titleMessage = self.sourceData[index];
  155. return titleMessage;
  156. }
  157. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  158. [pickerView selectRow:row inComponent:component animated:YES];
  159. _chooseIndex = row;
  160. }
  161. #pragma mark ---- UIPickerView delegate
  162. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
  163. UILabel *pickerLabel = (UILabel *)view;
  164. if (!pickerLabel) {
  165. pickerLabel = [[UILabel alloc] init];
  166. [pickerLabel setTextAlignment:NSTextAlignmentCenter];
  167. [pickerLabel setBackgroundColor:[UIColor clearColor]];
  168. [pickerLabel setFont:[UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium]];
  169. [pickerLabel setTextColor:HexRGB(0x444444)];
  170. }
  171. pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
  172. return pickerLabel;
  173. }
  174. /*
  175. // Only override drawRect: if you perform custom drawing.
  176. // An empty implementation adversely affects performance during animation.
  177. - (void)drawRect:(CGRect)rect {
  178. // Drawing code
  179. }
  180. */
  181. @end