全部产品

日期组件

更新时间:2020-08-19 14:34:38

效果图

接口说明

  • AUDatePicker.h
  1. //
  2. // ALPPicketView.h
  3. // TestCell
  4. //
  5. #import <UIKit/UIKit.h>
  6. @class AUDatePicker;
  7. @protocol AUDatePickerDelegate <UIPickerViewDataSource, UIPickerViewDelegate>
  8. /*
  9. * 点取消息时回调
  10. */
  11. - (void)cancelPickerView:(AUDatePicker *)pickerView;
  12. /*
  13. * 点完成时回调,选中项可通过 pickerView/Users/zhuwei/ios-phone-antui/ANTUI/Sources/Views/pickerView/AUDatePicker.h selectedRowInComponent 返回
  14. */
  15. - (void)selectedPickerView:(AUDatePicker *)pickerView;
  16. @end
  17. /*!
  18. @class AUDatePicker
  19. @abstract UIView
  20. @discussion 原框架封装的选择器,在原来系统控件上加上的去掉和完成按钮
  21. */
  22. @interface AUDatePicker : UIView
  23. @property(nonatomic, strong) UIPickerView *pickerView; // 通用事务选择器
  24. @property(nonatomic, strong) UIDatePicker *datePickerView; // 时间选择器
  25. @property(nonatomic, assign) BOOL isDatePicker; // 当前是否是时间选择器,默认为 NO
  26. @property(nonatomic, weak) id<AUDatePickerDelegate> delegate;
  27. /*
  28. * 创建组件
  29. *
  30. * @param title 标题,可为 nil
  31. * @return 创建的组件,默认不显示,需调用 show
  32. */
  33. + (AUDatePicker *)pickerViewWithTitle:(NSString *)title;
  34. /*
  35. * 初始化对象
  36. *
  37. * @param frame 显示位置
  38. * @param title 显示标题,不显示可设 nil
  39. * @return 默认返回对象不显示,要显示需要调 show
  40. */
  41. - (id)initWithFrame:(CGRect)frame withTitle:(NSString *)title;
  42. /*
  43. * 显示
  44. */
  45. - (void)show;
  46. /*
  47. * 隐藏
  48. */
  49. - (void)hide;
  50. /**
  51. * 重载数据
  52. */
  53. - (void)reload;
  54. /**
  55. 当 isDatePicker 为 YES 时,使用 datePickerView 选择时间
  56. @param minDate 最小时间
  57. @param maxDate 最大时间
  58. */
  59. - (void) setTimeDateminDate:(NSDate *)minDate MaxDate:(NSDate *)maxDate;
  60. /**
  61. 当 isDatePicker 为 YES 时,设置 datePickerView 的当前时间
  62. @param currentDate 设置当前的时间
  63. */
  64. - (void) setCurrentDate:(NSDate *) currentDate;
  65. /**
  66. 当 isDatePicker 为 YES 时,设置时间选择器中选择的时间
  67. @param date 选中的日期
  68. @param animated 是否包含动画
  69. */
  70. - (void)setAUDatePickerDate:(NSDate *)date animated:(BOOL)animated; // if animated is YES, animate the wheels of time to display the new date
  71. @end

代码示例

  1. //
  2. // APPickerViewViewController.m
  3. // UIDemo
  4. //
  5. #import "APPickerViewViewController.h"
  6. #import "AUDatePicker.h"
  7. @interface APPickerViewViewController ()<AUDatePickerDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
  8. @property(nonatomic,strong)AUDatePicker* apPickerView;
  9. @property(nonatomic,strong)AUDatePicker* apPickerView2;
  10. @property(nonatomic,strong)AUDatePicker* apPickerView3;
  11. @property(nonatomic,strong)AUDatePicker* apPickerView4;
  12. @property(nonatomic,strong)UILabel* textLabel;
  13. @property(nonatomic,strong)NSArray* yearArray;
  14. @property(nonatomic,strong)NSArray* monthArray;
  15. @property(nonatomic,strong)NSArray* nameArray;
  16. @end
  17. @implementation APPickerViewViewController
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  19. {
  20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  21. if (self) {
  22. // Custom initialization
  23. self.yearArray = @[@"2009",@"2010",@"2011",@"2012",@"2013",@"2014",@"2015",@"2016"];
  24. self.monthArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
  25. self.nameArray = @[@"赵一",@"钱二",@"孙三",@"李四",@"王五",@"张六",@"刘七"];
  26. }
  27. return self;
  28. }
  29. - (void)viewDidLoad
  30. {
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. [self.view setBackgroundColor:[UIColor whiteColor]];
  34. NSArray* items = @[@"类方法创建",@"成员方法创建",@"时间选择器1",@"时间选择器2"];
  35. UISegmentedControl* segmentControl = [[UISegmentedControl alloc]initWithItems:items];
  36. [segmentControl addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventValueChanged];
  37. segmentControl.selectedSegmentIndex = 0;
  38. [segmentControl setFrame:CGRectMake(15, 70, AUCommonUIGetScreenWidth() - 30, 30)];
  39. [self.view addSubview:segmentControl];
  40. //label 用来显示 pickerView 选择的项目
  41. self.textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 110, 220, 50)];
  42. self.textLabel.frame = CGRectOffset(self.textLabel.frame, (AUCommonUIGetScreenWidth()-self.textLabel.frame.size.width)/2, 0);
  43. self.textLabel.layer.cornerRadius = 12.f;
  44. self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
  45. self.textLabel.numberOfLines = 0;
  46. self.textLabel.textAlignment = NSTextAlignmentCenter;
  47. [self.view addSubview:self.textLabel];
  48. //类方法创建的 pickerView
  49. self.apPickerView = [AUDatePicker pickerViewWithTitle:nil];
  50. self.apPickerView.delegate = self;
  51. self.apPickerView.tag = 1000;
  52. [self.view addSubview:self.apPickerView];
  53. [self.apPickerView show];
  54. //成员方法创建的 pickerView
  55. _apPickerView2 = [[AUDatePicker alloc]initWithFrame:CGRectMake(0, 200, 200, 200) withTitle:nil];
  56. _apPickerView2.delegate = self;
  57. _apPickerView2.tag = 1001;
  58. [self.view addSubview:_apPickerView2];
  59. //时间选择器 1
  60. self.apPickerView3 = [AUDatePicker pickerViewWithTitle:@"请选择时间"];
  61. self.apPickerView3.tag = 1002;
  62. self.apPickerView3.isDatePicker = YES;
  63. NSDate * curretntDate = [NSDate date];
  64. NSDate * minxDate = [NSDate dateWithTimeInterval:-(3600*24*3000) sinceDate:curretntDate];
  65. NSDate * maxDate = [NSDate dateWithTimeInterval:3600*24*3000 sinceDate:curretntDate];
  66. [self.apPickerView3 setTimeDateminDate:minxDate MaxDate:maxDate];
  67. [self.apPickerView3 setCurrentDate:curretntDate];
  68. [self.view addSubview:self.apPickerView3];
  69. //时间选择器 2
  70. self.apPickerView4 = [AUDatePicker pickerViewWithTitle:@"请选择时间"];
  71. self.apPickerView4.tag = 1003;
  72. self.apPickerView4.isDatePicker = YES;
  73. [self.apPickerView4 setTimeDateminDate:minxDate MaxDate:maxDate];
  74. [self.apPickerView4 setCurrentDate:curretntDate];
  75. NSDate * selectDate =[NSDate dateWithTimeInterval:3600*24*888 sinceDate:curretntDate];
  76. [self.apPickerView4 setAUDatePickerDate:selectDate animated:NO];
  77. [self.view addSubview:self.apPickerView4];
  78. // self.navigationItem.rightBarButtonItem = [APUtil getBarButtonWithTitle:RightBarButtonTitle target:self];
  79. }
  80. - (void)didReceiveMemoryWarning
  81. {
  82. [super didReceiveMemoryWarning];
  83. // Dispose of any resources that can be recreated.
  84. }
  85. #pragma mark - Button onClick
  86. - (void)onBarButtonClick:(id)sender
  87. {
  88. }
  89. - (void)onClick:(id)sender
  90. {
  91. [self.apPickerView hide];
  92. [self.apPickerView2 hide];
  93. [self.apPickerView3 hide];
  94. [self.apPickerView4 hide];
  95. UISegmentedControl* segmentControl = (UISegmentedControl*)sender;
  96. switch (segmentControl.selectedSegmentIndex) {
  97. case 0:
  98. [self.apPickerView show];
  99. break;
  100. case 1:
  101. [self.apPickerView2 show];
  102. break;
  103. case 2:
  104. [self.apPickerView3 show];
  105. break;
  106. case 3:
  107. [self.apPickerView4 show];
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. #pragma APPickerDelegate delegate
  114. - (void)cancelPickerView:(AUDatePicker *)pickerView
  115. {
  116. switch (pickerView.tag) {
  117. case 1000:
  118. [self.apPickerView hide];
  119. break;
  120. case 1001:
  121. [self.apPickerView2 hide];
  122. break;
  123. case 1002:
  124. [self.apPickerView3 hide];
  125. break;
  126. case 1003:
  127. [self.apPickerView4 hide];
  128. break;
  129. default:
  130. break;
  131. }
  132. [self.textLabel setText:@"点击“取消”按钮时的回调"];
  133. }
  134. - (void)selectedPickerView:(AUDatePicker *)pickerView
  135. {
  136. NSInteger index = [pickerView.pickerView selectedRowInComponent:0];
  137. NSString *result = [self.yearArray objectAtIndex:index];
  138. index = [pickerView.pickerView selectedRowInComponent:1];
  139. result = [result stringByAppendingString:[NSString stringWithFormat:@" %@",[self.monthArray objectAtIndex:index]]];
  140. index = [pickerView.pickerView selectedRowInComponent:2];
  141. result = [result stringByAppendingString:[NSString stringWithFormat:@" %@",[self.nameArray objectAtIndex:index]]];
  142. [self.textLabel setText:result];
  143. }
  144. #pragma UIPickerView delegate
  145. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
  146. {
  147. if (component == 0) {
  148. return [self.yearArray objectAtIndex:row];
  149. } else if (component == 1){
  150. return [self.monthArray objectAtIndex:row];
  151. } else {
  152. return [self.nameArray objectAtIndex:row];
  153. }
  154. }
  155. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  156. {
  157. return 3;
  158. }
  159. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  160. {
  161. if (component == 0) {
  162. return [self.yearArray count];
  163. } else if (component == 1){
  164. return [self.monthArray count];
  165. } else {
  166. return [self.nameArray count];
  167. }
  168. }
  169. @end