birthdayView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // birthdayView.m
  3. // BuguLive
  4. //
  5. // Created by fanwe2014 on 16/7/19.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "birthdayView.h"
  9. #define CANCLE_LABEL_TAG 11
  10. #define CONFRM_LABEL_TAG 12
  11. @interface birthdayView ()<UIPickerViewDataSource,UIPickerViewDelegate>
  12. @property (nonatomic, assign) NSInteger year;
  13. @property (nonatomic, assign) NSInteger month;
  14. @property (nonatomic, assign) NSInteger day;
  15. @end
  16. @implementation birthdayView
  17. - (id)initWithDelegate:(id<LZDateDelegate>)delegate year:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)day
  18. {
  19. if (self = [super init])
  20. {
  21. self.backgroundColor = kBackGroundColor;
  22. self.delegate = delegate;
  23. self.month = month;
  24. self.year = year;
  25. self.day = day;
  26. [self createView];
  27. }
  28. return self;
  29. }
  30. - (void)createView
  31. {
  32. UILabel *cancle = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 60, 30)];
  33. cancle.userInteractionEnabled = YES;
  34. cancle.text = ASLocalizedString(@"取消");
  35. cancle.tag = CANCLE_LABEL_TAG;
  36. cancle.textColor = [UIColor grayColor];
  37. cancle.textAlignment = NSTextAlignmentCenter;
  38. [self addSubview:cancle];
  39. UITapGestureRecognizer *canTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
  40. [cancle addGestureRecognizer:canTap];
  41. UILabel *confrm = [[UILabel alloc] initWithFrame:CGRectMake(kScreenW - 70, 5, 60, 30)];
  42. confrm.userInteractionEnabled = YES;
  43. confrm.text = ASLocalizedString(@"确认");
  44. confrm.tag = CONFRM_LABEL_TAG;
  45. confrm.textColor = [UIColor grayColor];
  46. confrm.textAlignment = NSTextAlignmentCenter;
  47. [self addSubview:confrm];
  48. UITapGestureRecognizer *conTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
  49. [confrm addGestureRecognizer:conTap];
  50. NSDate *currentDate = [NSDate date];//获取当前时间,日期
  51. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  52. [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss SS"];
  53. NSString *dateString = [dateFormatter stringFromDate:currentDate];
  54. _yearNum = [[dateString substringWithRange:NSMakeRange(0,4)] intValue];
  55. UIPickerView *pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 35, kScreenW, 135)];
  56. pickView.delegate = self;
  57. pickView.dataSource = self;
  58. [pickView selectRow:_yearNum-self.year inComponent:0 animated:YES];
  59. [pickView selectRow:self.month-1 inComponent:1 animated:YES];
  60. [pickView selectRow:self.day-1 inComponent:2 animated:YES];
  61. [self addSubview:pickView];
  62. }
  63. // 一共有多少列
  64. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  65. {
  66. return 3;
  67. }
  68. // 第component列一共有多少行
  69. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  70. {
  71. if(component == 0)
  72. {
  73. return 200;
  74. }
  75. else if (component == 1)
  76. {
  77. return 12;
  78. }
  79. else
  80. {
  81. if(self.month == 2)
  82. {
  83. if ( ((self.year % 4 == 0) && (self.year % 100 != 0))|| (self.year % 400 == 0))
  84. {
  85. return 29;
  86. }
  87. else
  88. {
  89. return 28;
  90. }
  91. }
  92. else if((self.month == 4 )|| (self.month == 6) || (self.month == 9) || (self.month == 11))
  93. {
  94. return 30;
  95. }
  96. else
  97. {
  98. return 31;
  99. }
  100. }
  101. }
  102. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  103. {
  104. if(component == 0)
  105. {
  106. self.year = _yearNum - row;
  107. }
  108. else if(component == 1)
  109. {
  110. self.month = row+1;
  111. }
  112. else if(component == 2)
  113. {
  114. self.day = row+1;
  115. }
  116. [pickerView reloadAllComponents];
  117. }
  118. //自定义 当前列 当前行 要显示的内容
  119. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
  120. {
  121. UILabel *myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreenW/3 , 30)];
  122. myView.textAlignment = NSTextAlignmentCenter;
  123. myView.font = [UIFont systemFontOfSize:14];
  124. if(component == 0)
  125. {
  126. myView.text = [NSString stringWithFormat:ASLocalizedString(@"%ld年"),_yearNum - row];
  127. }
  128. else if (component == 1)
  129. {
  130. if (row < 9)
  131. {
  132. myView.text = [NSString stringWithFormat:ASLocalizedString(@"0%ld月"),row + 1];
  133. }
  134. myView.text = [NSString stringWithFormat:ASLocalizedString(@"%ld月"),row + 1];
  135. }
  136. else if (component == 2)
  137. {
  138. if (row)
  139. {
  140. myView.text = [NSString stringWithFormat:ASLocalizedString(@"0%ld日"),row + 1];
  141. }
  142. myView.text = [NSString stringWithFormat:ASLocalizedString(@"%ld日"),row + 1];
  143. }
  144. return myView;
  145. }
  146. //第component列的宽度是多少
  147. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
  148. {
  149. return kScreenW/3 ;
  150. }
  151. //第component列的行高是多少
  152. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
  153. {
  154. return 30;
  155. }
  156. - (void)tapClick:(UITapGestureRecognizer *)tap
  157. {
  158. if(self.delegate)
  159. {
  160. int myTag = (int)tap.view.tag;
  161. if([self.delegate respondsToSelector:@selector(confrmCallBack:month:day:andtag:)] == YES)
  162. {
  163. [self.delegate confrmCallBack:self.year month:self.month day:self.day andtag:myTag];
  164. }
  165. }
  166. }
  167. @end