PickerView.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // PickerView.m
  3. // TCXF
  4. //
  5. // Created by 张毅成 on 2017/7/6.
  6. // Copyright © 2017年 张毅成. All rights reserved.
  7. //
  8. #import "PickerView.h"
  9. @implementation PickerView
  10. - (instancetype)init {
  11. if (self = [super init]) {
  12. [self makeUI];
  13. [self makeToolBar];
  14. }
  15. return self;
  16. }
  17. - (void)makeUI {
  18. _selectIndex = 0;
  19. self.delegate = self;
  20. self.dataSource = self;
  21. self.frame = CGRectMake(0,kGXScreenHeigh * 1.1, kGXScreenWidth, kGXScreenHeigh / 2);
  22. self.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
  23. }
  24. - (void)makeToolBar {
  25. UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, kGXScreenHeigh * 1.1, kGXScreenWidth, 40)];
  26. toolBar.backgroundColor = [UIColor colorWithHexString:@"666666"];
  27. NSMutableArray *barItems = [NSMutableArray array];
  28. UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"\t取消" style:UIBarButtonItemStylePlain target:self action:@selector(pickerViewDisappear)];
  29. [cancelBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: kMainColor, NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
  30. [barItems addObject:cancelBtn];
  31. UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  32. [barItems addObject:flexSpace];
  33. UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定\t" style:UIBarButtonItemStylePlain target:self action:@selector(pickerViewDone)];
  34. [doneBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: kMainColor, NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
  35. [barItems addObject:doneBtn];
  36. toolBar.items = barItems;
  37. self.toolBar = toolBar;
  38. }
  39. - (void)showView {
  40. WeakSelf(self)
  41. [self pickerView:self didSelectRow:self.selectIndex inComponent:0];
  42. [[UIApplication sharedApplication].keyWindow addSubview:self];
  43. [[UIApplication sharedApplication].keyWindow addSubview:self.toolBar];
  44. [UIView animateWithDuration:0.4 animations:^{
  45. StrongSelf(weakself)
  46. strongself.hidden = false;
  47. strongself.toolBar.hidden = false;
  48. strongself.frame = CGRectMake(0,kGXScreenHeigh / 2, kGXScreenWidth, kGXScreenHeigh / 2);
  49. strongself.toolBar.frame = CGRectMake(strongself.frame.origin.x, strongself.frame.origin.y, strongself.width, 40);
  50. } completion:^(BOOL finished) {}];
  51. }
  52. - (void)showinView:(UIView *)view {
  53. WeakSelf(self)
  54. [view addSubview:self];
  55. [view addSubview:self.toolBar];
  56. [UIView animateWithDuration:0.4 animations:^{
  57. StrongSelf(weakself)
  58. strongself.hidden = false;
  59. strongself.toolBar.hidden = false;
  60. strongself.frame = CGRectMake(0,view.height - kGXScreenHeigh*0.5, kGXScreenWidth, kGXScreenHeigh*0.5);
  61. strongself.toolBar.frame = CGRectMake(strongself.frame.origin.x, strongself.frame.origin.y, strongself.width, 40);
  62. } completion:^(BOOL finished) {}];
  63. }
  64. - (void)pickerViewDone {
  65. WeakSelf(self)
  66. [UIView animateWithDuration:0.3 animations:^{
  67. StrongSelf(weakself)
  68. strongself.frame = CGRectMake(0, 1000, strongself.width, 40);
  69. strongself.toolBar.frame = CGRectMake(0, 1000, strongself.width, strongself.toolBar.height);
  70. } completion:^(BOOL finished) {
  71. StrongSelf(weakself)
  72. if (self.block) {
  73. self.block(self.title,_selectIndex);
  74. }
  75. strongself.hidden = YES;
  76. strongself.toolBar.hidden = YES;
  77. [strongself removeFromSuperview];
  78. [strongself.toolBar removeFromSuperview];
  79. }];
  80. }
  81. - (void)pickerViewDisappear {
  82. WeakSelf(self)
  83. [UIView animateWithDuration:0.3 animations:^{
  84. StrongSelf(weakself)
  85. strongself.frame = CGRectMake(0, 1000, strongself.width, 40);
  86. strongself.toolBar.frame = CGRectMake(0, 1000, strongself.width, strongself.toolBar.height);
  87. } completion:^(BOOL finished) {
  88. StrongSelf(weakself)
  89. strongself.hidden = YES;
  90. strongself.toolBar.hidden = YES;
  91. [strongself removeFromSuperview];
  92. [strongself.toolBar removeFromSuperview];
  93. }];
  94. }
  95. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  96. return 1;
  97. }
  98. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  99. return 2;
  100. }
  101. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  102. return @[@"男", @"女"][row];
  103. }
  104. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  105. self.title = @[@"男", @"女"][row];
  106. self.selectIndex = row;
  107. }
  108. - (BOOL)respondsToSelector:(SEL)aSelector {
  109. return [super respondsToSelector:aSelector];
  110. }
  111. @end