UIPopoverListView.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //
  2. // UIPopoverListView.m
  3. // UIPopoverListViewDemo
  4. //
  5. // Created by su xinde on 13-3-13.
  6. // Copyright (c) 2013年 su xinde. All rights reserved.
  7. //
  8. #import "UIPopoverListView.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. //#define FRAME_X_INSET 20.0f
  11. //#define FRAME_Y_INSET 40.0f
  12. @interface UIPopoverListView ()
  13. //- (void)defalutInit;
  14. - (void)fadeIn;
  15. - (void)fadeOut;
  16. @end
  17. @implementation UIPopoverListView
  18. NSString *popoverDateStr;
  19. //view
  20. UIView *popDetailView;
  21. @synthesize datasource = _datasource;
  22. @synthesize delegate = _delegate;
  23. @synthesize listView = _listView;
  24. @synthesize datePickerView=_datePickerView;
  25. - (id)initWithFrame:(CGRect)frame isDateView:(BOOL)isDateView
  26. {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. [self defalutInit:isDateView];
  30. }
  31. return self;
  32. }
  33. - (id)initWithFrame:(CGRect)frame forTextView:(BOOL)isTextView
  34. {
  35. self = [super initWithFrame:frame];
  36. if (self) {
  37. [self textViewDefalutInit:isTextView];
  38. }
  39. return self;
  40. }
  41. - (id)initWithFrame:(CGRect)frame forMapView:(NSString *)webViewLink
  42. {
  43. self = [super initWithFrame:frame];
  44. if (self) {
  45. [self webViewDefalutInit:webViewLink];
  46. }
  47. return self;
  48. }
  49. - (id)initWithFrame:(CGRect)frame imageName:(NSString*)name
  50. {
  51. self = [super initWithFrame:frame];
  52. if (self) {
  53. [self imageViewDefalutInit:name];
  54. }
  55. return self;
  56. }
  57. - (void)imageViewDefalutInit:(NSString*)name
  58. {
  59. self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
  60. self.layer.borderWidth = 1.0f;
  61. self.layer.cornerRadius = 10.0f;
  62. self.clipsToBounds = TRUE;
  63. _titleView = [[UILabel alloc] initWithFrame:CGRectZero];
  64. _titleView.font = FONT_SIZE(15);
  65. _titleView.backgroundColor = HexColor(@"#4388B7");
  66. _titleView.textAlignment = NSTextAlignmentCenter;
  67. _titleView.textColor = [UIColor whiteColor];
  68. CGFloat xWidth = self.bounds.size.width;
  69. _titleView.lineBreakMode = NSLineBreakByCharWrapping;
  70. _titleView.frame = CGRectMake(0, 0, xWidth, 32.0f);
  71. [self addSubview:_titleView];
  72. UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 32.0f, self.bounds.size.width, self.bounds.size.height-32.0f)];
  73. view.backgroundColor=[UIColor whiteColor];
  74. [self addSubview:view];
  75. //UIImageView
  76. UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150,150)];
  77. imageView.center=CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2+17);
  78. imageView.image=[UIImage imageNamed:name];
  79. [self addSubview:imageView];
  80. _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  81. _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.8];
  82. if (self.isCannotClickBackGround) {
  83. [_overlayView addTarget:self
  84. action:@selector(dismiss)
  85. forControlEvents:UIControlEventTouchUpInside];
  86. }
  87. }
  88. - (void)webViewDefalutInit:(NSString *)webViewLink
  89. {
  90. self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
  91. self.layer.borderWidth = 1.0f;
  92. self.layer.cornerRadius = 10.0f;
  93. self.clipsToBounds = TRUE;
  94. _titleView = [[UILabel alloc] initWithFrame:CGRectZero];
  95. _titleView.font = FONT_SIZE(15);
  96. _titleView.backgroundColor = HexColor(@"#4388B7");
  97. _titleView.textAlignment = NSTextAlignmentCenter;
  98. _titleView.textColor = [UIColor whiteColor];
  99. CGFloat xWidth = self.bounds.size.width;
  100. _titleView.lineBreakMode = NSLineBreakByCharWrapping;
  101. _titleView.frame = CGRectMake(0, 0, xWidth, 32.0f);
  102. [self addSubview:_titleView];
  103. //view
  104. popDetailView=[[UIView alloc] initWithFrame:CGRectMake(0, 32.0f, self.bounds.size.width, self.bounds.size.height-32.0f)];
  105. popDetailView.backgroundColor=[UIColor whiteColor];
  106. [self addSubview:popDetailView];
  107. //textView
  108. _webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 32, self.bounds.size.width, self.bounds.size.height)];
  109. [self addSubview:_webView];
  110. _webView.delegate=self;
  111. _webView.scalesPageToFit=YES;
  112. NSMutableURLRequest *preRequest;
  113. DLog(@"popWebView=%@",webViewLink);
  114. preRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:webViewLink]];
  115. [_webView loadRequest:preRequest];
  116. _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  117. _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.8
  118. ];
  119. if (self.isCannotClickBackGround) {
  120. [_overlayView addTarget:self
  121. action:@selector(dismiss)
  122. forControlEvents:UIControlEventTouchUpInside];
  123. }
  124. }
  125. - (void)textViewDefalutInit:(BOOL)isTextView
  126. {
  127. self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
  128. self.layer.borderWidth = 1.0f;
  129. self.layer.cornerRadius = 10.0f;
  130. self.clipsToBounds = TRUE;
  131. _titleView = [[UILabel alloc] initWithFrame:CGRectZero];
  132. _titleView.font = FONT_SIZE(15);
  133. _titleView.backgroundColor = HexColor(@"#4388B7");
  134. _titleView.textAlignment = NSTextAlignmentCenter;
  135. _titleView.textColor = [UIColor whiteColor];
  136. CGFloat xWidth = self.bounds.size.width;
  137. _titleView.lineBreakMode = NSLineBreakByCharWrapping;
  138. _titleView.frame = CGRectMake(0, 0, xWidth, 32.0f);
  139. [self addSubview:_titleView];
  140. //view
  141. popDetailView=[[UIView alloc] initWithFrame:CGRectMake(0, 32.0f, self.bounds.size.width, self.bounds.size.height-32.0f)];
  142. popDetailView.backgroundColor=[UIColor whiteColor];
  143. [self addSubview:popDetailView];
  144. //textView
  145. self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 32.0f, self.bounds.size.width, self.bounds.size.height-32.0f)];//初始化大小并自动释放
  146. self.textView.textColor = [UIColor blackColor];//设置textview里面的字体颜色
  147. self.textView.font = [UIFont fontWithName:@"Arial" size:15.0];//设置字体名字和字体大小
  148. //self.textView.delegate = self;//设置它的委托方法
  149. self.textView.backgroundColor = [UIColor whiteColor];//设置它的背景颜色
  150. //self.textView.text = @"请在此处输入:";//设置它显示的内容
  151. self.textView.returnKeyType = UIReturnKeyDefault;//返回键的类型
  152. self.textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
  153. self.textView.scrollEnabled = YES;//是否可以拖动
  154. UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
  155. label.text=@"请在此处输入:";
  156. label.tag=1;
  157. [self.textView addSubview:label];
  158. [self addSubview:self.textView];
  159. _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  160. _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.8
  161. ];
  162. if (self.isCannotClickBackGround) {
  163. [_overlayView addTarget:self
  164. action:@selector(dismiss)
  165. forControlEvents:UIControlEventTouchUpInside];
  166. }
  167. }
  168. - (void)defalutInit:(BOOL)isDateView
  169. {
  170. self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
  171. self.layer.borderWidth = 1.0f;
  172. self.layer.cornerRadius = 10.0f;
  173. self.clipsToBounds = TRUE;
  174. _titleView = [[UILabel alloc] initWithFrame:CGRectZero];
  175. _titleView.font = FONT_SIZE(15);
  176. _titleView.backgroundColor = HexColor(@"#4388B7");
  177. _titleView.textAlignment = NSTextAlignmentCenter;
  178. _titleView.textColor = [UIColor whiteColor];
  179. CGFloat xWidth = self.bounds.size.width;
  180. _titleView.lineBreakMode = NSLineBreakByCharWrapping;
  181. _titleView.frame = CGRectMake(0, 0, xWidth, 32.0f);
  182. [self addSubview:_titleView];
  183. //tableview
  184. //////////////////////////////////////
  185. if (isDateView) {
  186. UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 32, xWidth , self.bounds.size.height-32.0f)];
  187. view.backgroundColor=[UIColor whiteColor];
  188. [self addSubview:view];
  189. CGRect datePickerCR = CGRectMake(0, 32.0f, xWidth, self.bounds.size.height-32.0f);
  190. _datePickerView=[[UIDatePicker alloc] initWithFrame:datePickerCR];
  191. [_datePickerView addTarget:self action:@selector(dateAction:) forControlEvents:UIControlEventValueChanged];
  192. //设置中文区域化
  193. NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
  194. _datePickerView.locale=locale;
  195. //[_datePickerView selectRow:0 inComponent:0 animated:NO];
  196. [_datePickerView setDatePickerMode:UIDatePickerModeDate];
  197. [self addSubview:_datePickerView];
  198. //加一个按钮
  199. CGRect frame1 =CGRectMake(self.bounds.size.width/2-30, self.bounds.size.height-40.0f, 60, 30.0f);
  200. UIImage *buttonImageNomal=[UIImage imageNamed:@"changBlue.png"];
  201. UIImage *stretchableButtonImageNomal=[buttonImageNomal stretchableImageWithLeftCapWidth:12 topCapHeight:1];
  202. UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  203. button1.frame = frame1;
  204. [button1 setTitle:@"确定" forState: UIControlStateNormal];
  205. button1.backgroundColor = [UIColor clearColor];
  206. button1.tag = 2001;
  207. [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  208. [button1 addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
  209. [button1 setBackgroundImage:stretchableButtonImageNomal forState:UIControlStateNormal];
  210. [self addSubview:button1];
  211. }else
  212. {
  213. CGRect tableFrame = CGRectMake(0, 32.0f, xWidth, self.bounds.size.height-32.0f);
  214. _listView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
  215. _listView.dataSource = self;
  216. _listView.delegate = self;
  217. [self addSubview:_listView];
  218. }
  219. _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  220. _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
  221. if (self.isCannotClickBackGround) {
  222. [_overlayView addTarget:self
  223. action:@selector(dismiss)
  224. forControlEvents:UIControlEventTouchUpInside];
  225. }
  226. }
  227. -(void)setIsCannotClickBackGround:(BOOL)isCannotClickBackGround{
  228. if (isCannotClickBackGround) {
  229. [_overlayView addTarget:self
  230. action:@selector(dismiss)
  231. forControlEvents:UIControlEventTouchUpInside];
  232. }
  233. }
  234. #pragma mark -UIDatePicker
  235. //日期确定按钮
  236. BOOL isfirstOpen=YES;
  237. -(void)dateAction:(id)sender{
  238. UIDatePicker *control=(UIDatePicker *)sender;
  239. NSDate* date=control.date;
  240. NSDateFormatter * df2 = [[NSDateFormatter alloc] init];
  241. [df2 setDateFormat:@"yyyy-MM-dd"];
  242. NSString * str1 = [df2 stringFromDate:date];
  243. popoverDateStr=str1;
  244. [self.delegate setDateText:popoverDateStr];
  245. isfirstOpen=NO;
  246. DLog(@"date=%@",str1);
  247. }
  248. -(void)buttonClicked1:(id)sender{
  249. // [self.delegate setDateText:popoverDateStr];
  250. if (isfirstOpen) {
  251. [self dateAction:nil];
  252. }
  253. if (self.dateBlock) {
  254. self.dateBlock();
  255. }
  256. [self dismiss];
  257. }
  258. #pragma mark - UITableViewDataSource
  259. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  260. {
  261. if (self.datasource&&
  262. [self.datasource respondsToSelector:@selector(numberOfSectionsInUIPopoverListView:)]) {
  263. return [self.datasource numberOfSectionsInUIPopoverListView:self];
  264. }
  265. return 4;
  266. }
  267. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  268. {
  269. if(self.datasource &&
  270. [self.datasource respondsToSelector:@selector(popoverListView:numberOfRowsInSection:)])
  271. {
  272. return [self.datasource popoverListView:self numberOfRowsInSection:section];
  273. }
  274. return 0;
  275. }
  276. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  277. {
  278. if(self.datasource &&
  279. [self.datasource respondsToSelector:@selector(popoverListView:cellForIndexPath:)])
  280. {
  281. return [self.datasource popoverListView:self cellForIndexPath:indexPath];
  282. }
  283. return nil;
  284. }
  285. -(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
  286. {
  287. //return [stories objectAtIndex:section];
  288. if(self.datasource &&
  289. [self.datasource respondsToSelector:@selector(popoverListView:titleForHeaderInSection:)])
  290. {
  291. return [self.datasource popoverListView:self titleForHeaderInSection:section];
  292. }
  293. return @"";
  294. }
  295. #pragma mark - UITableViewDelegate
  296. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  297. {
  298. if(self.delegate &&
  299. [self.delegate respondsToSelector:@selector(popoverListView:heightForRowAtIndexPath:)])
  300. {
  301. return [self.delegate popoverListView:self heightForRowAtIndexPath:indexPath];
  302. }
  303. return 0.0f;
  304. }
  305. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  306. {
  307. if(self.delegate &&
  308. [self.delegate respondsToSelector:@selector(popoverListView:didSelectIndexPath:)])
  309. {
  310. [self.delegate popoverListView:self didSelectIndexPath:indexPath];
  311. }
  312. [self dismiss];
  313. }
  314. #pragma mark --- 显示完整分割线
  315. -(void)viewDidLayoutSubviews {
  316. if ([_listView respondsToSelector:@selector(setSeparatorInset:)]) {
  317. [_listView setSeparatorInset:UIEdgeInsetsZero];
  318. }
  319. if ([_listView respondsToSelector:@selector(setLayoutMargins:)]) {
  320. [_listView setLayoutMargins:UIEdgeInsetsZero];
  321. }
  322. }
  323. #pragma mark - animations
  324. - (void)fadeIn
  325. {
  326. self.transform = CGAffineTransformMakeScale(1.3, 1.3);
  327. self.alpha = 0;
  328. [UIView animateWithDuration:.35 animations:^{
  329. self.alpha = 1;
  330. self.transform = CGAffineTransformMakeScale(1, 1);
  331. }];
  332. }
  333. - (void)fadeOut
  334. {
  335. [UIView animateWithDuration:.35 animations:^{
  336. self.transform = CGAffineTransformMakeScale(1.3, 1.3);
  337. self.alpha = 0.0;
  338. } completion:^(BOOL finished) {
  339. if (finished) {
  340. [self->_overlayView removeFromSuperview];
  341. [self removeFromSuperview];
  342. }
  343. }];
  344. }
  345. - (void)setTitle:(NSString *)title
  346. {
  347. _titleView.text = title;
  348. }
  349. - (void)show
  350. {
  351. UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
  352. [keywindow addSubview:_overlayView];
  353. [keywindow addSubview:self];
  354. self.center = CGPointMake(keywindow.bounds.size.width/2.0f,self.frame.origin.y+self.frame.size.height/2);
  355. // keywindow.bounds.size.height/2.0f);
  356. [self fadeIn];
  357. }
  358. - (void)dismiss
  359. {
  360. [self fadeOut];
  361. isfirstOpen=YES;
  362. if (self.BackBlock) {
  363. self.BackBlock();
  364. }
  365. }
  366. #define mark - UITouch
  367. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  368. {
  369. // tell the delegate the cancellation
  370. if (self.delegate && [self.delegate respondsToSelector:@selector(popoverListViewCancel:)]) {
  371. [self.delegate popoverListViewCancel:self];
  372. }
  373. DLog(@"Cancel!!!!!!!");
  374. // dismiss self
  375. [self dismiss];
  376. }
  377. @end