Bez popisu

SubmitDiscountPickView.m 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // SubmitDiscountPickView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/6/20.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. // 选择代金券
  8. #import "SubmitDiscountPickView.h"
  9. #define kContentHeight 194
  10. @interface SubmitDiscountPickView ()<UIPickerViewDataSource, UIPickerViewDelegate>
  11. @property (nonatomic, strong) UIView *contentView;
  12. @property (nonatomic, strong) UIView *bannerView;
  13. @property (nonatomic, strong) NSArray *titleArray;
  14. @end
  15. @implementation SubmitDiscountPickView
  16. @synthesize pickerView = _pickerView;
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. if (self = [super initWithFrame:frame]) {
  20. [self initializeLayout];
  21. [self addTapGesture];
  22. self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
  23. }
  24. return self;
  25. }
  26. - (void)addTapGesture{
  27. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTap:)];
  28. [self addGestureRecognizer:tap];
  29. }
  30. - (void)initializeLayout
  31. {
  32. UIButton *cancelBtn = ({
  33. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  34. [button setTitle:@"取消" forState:UIControlStateNormal];
  35. [button setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
  36. [button addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
  37. button.titleLabel.font = [UIFont systemFontOfSize:14];
  38. button;
  39. });
  40. UIButton *confirmBtn = ({
  41. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  42. [button setTitle:@"完成" forState:UIControlStateNormal];
  43. [button setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
  44. [button addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
  45. button.titleLabel.font = [UIFont systemFontOfSize:14];
  46. button;
  47. });
  48. [self addSubview:self.contentView];
  49. [self.contentView addSubview:self.bannerView];
  50. [self.contentView addSubview:self.pickerView];
  51. [self.bannerView addSubview:cancelBtn];
  52. [self.bannerView addSubview:confirmBtn];
  53. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.left.right.bottom.equalTo(self);
  55. make.height.equalTo(@kContentHeight);
  56. }];
  57. [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.left.right.top.equalTo(self.contentView);
  59. make.height.equalTo(@44);
  60. }];
  61. [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
  62. make.top.equalTo(self.bannerView.mas_bottom);
  63. make.left.right.bottom.equalTo(self.contentView);
  64. }];
  65. [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.left.equalTo(self.bannerView);
  67. make.height.equalTo(self.bannerView);
  68. make.centerY.equalTo(self.bannerView);
  69. make.width.equalTo(@80);
  70. }];
  71. [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.right.equalTo(self.bannerView);
  73. make.height.equalTo(self.bannerView);
  74. make.centerY.equalTo(self.bannerView);
  75. make.width.equalTo(cancelBtn);
  76. }];
  77. }
  78. - (void)showInView:(UIView *)view animated:(BOOL)animated
  79. {
  80. if (!view) return;
  81. [view addSubview:self];
  82. self.frame = view.bounds;
  83. WeakSelf(weakSelf);
  84. if (animated) {
  85. self.backgroundColor = [UIColor clearColor];
  86. CGAffineTransform transForm = CGAffineTransformMakeTranslation(0, kContentHeight);
  87. self.contentView.transform = transForm;
  88. [UIView animateWithDuration:0.3f animations:^{
  89. weakSelf.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
  90. weakSelf.contentView.transform = CGAffineTransformIdentity;
  91. }];
  92. }
  93. }
  94. - (void)dismissAnimated
  95. {
  96. CGRect frame = self.contentView.frame;
  97. CGRect downFrame = CGRectOffset(frame, 0, CGRectGetHeight(frame));
  98. self.contentView.transform = CGAffineTransformIdentity;
  99. WeakSelf(weakSelf);
  100. [UIView animateWithDuration:0.3f animations:^{
  101. weakSelf.backgroundColor = [UIColor clearColor];
  102. weakSelf.contentView.frame = downFrame;
  103. } completion:^(BOOL finished) {
  104. [weakSelf removeFromSuperview];
  105. weakSelf.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
  106. weakSelf.contentView.frame = frame;
  107. }];
  108. }
  109. - (BOOL)isShowing{
  110. if (self.superview) return YES;
  111. return NO;
  112. }
  113. - (void)refreshTitleArray:(NSArray *)titleArray withSelectedIndex:(NSInteger)index
  114. {
  115. _titleArray = titleArray;
  116. [self.pickerView reloadAllComponents];
  117. [self.pickerView selectRow:index inComponent:0 animated:NO];
  118. }
  119. #pragma mark - event response
  120. - (void)clickTap:(UITapGestureRecognizer *)gesture{
  121. [self dismissAnimated];
  122. }
  123. - (void)cancelBtnClick
  124. {
  125. [self dismissAnimated];
  126. }
  127. - (void)confirmBtnClick
  128. {
  129. NSInteger selectedRow = [self.pickerView selectedRowInComponent:0];
  130. if (_selectDone) {
  131. _selectDone(selectedRow);
  132. }
  133. [self dismissAnimated];
  134. }
  135. #pragma mark - pickerView dataSource & delegate
  136. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  137. {
  138. return 1;
  139. }
  140. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  141. {
  142. return self.titleArray.count;
  143. }
  144. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
  145. {
  146. return UISCREENWIDTH;
  147. }
  148. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
  149. {
  150. return 50;
  151. }
  152. - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
  153. return self.titleArray[row];
  154. }
  155. #pragma mark - getter && setter
  156. - (UIPickerView *)pickerView
  157. {
  158. if (_pickerView == nil) {
  159. _pickerView = [[UIPickerView alloc]init];
  160. _pickerView.dataSource = self;
  161. _pickerView.delegate = self;
  162. _pickerView.backgroundColor = [UIColor whiteColor];
  163. }
  164. return _pickerView;
  165. }
  166. - (UIView *)contentView
  167. {
  168. if (_contentView == nil) {
  169. _contentView = [[UIView alloc]init];
  170. _contentView.backgroundColor = [UIColor clearColor];
  171. }
  172. return _contentView;
  173. }
  174. - (UIView *)bannerView
  175. {
  176. if (_bannerView == nil) {
  177. _bannerView = [[UIView alloc]init];
  178. _bannerView.backgroundColor = [UIColor colorWithRed:235.0/255.0
  179. green:236.0/255.0
  180. blue:239.0/255.0
  181. alpha:1.0];
  182. }
  183. return _bannerView;
  184. }
  185. - (void)setTitleArray:(NSArray *)titleArray
  186. {
  187. _titleArray = titleArray;
  188. [self.pickerView reloadAllComponents];
  189. }
  190. @end