| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- //
- // UIPopoverListView.m
- // UIPopoverListViewDemo
- //
- // Created by su xinde on 13-3-13.
- // Copyright (c) 2013年 su xinde. All rights reserved.
- //
- #import "UIPopoverListView.h"
- #import <QuartzCore/QuartzCore.h>
- //#define FRAME_X_INSET 20.0f
- //#define FRAME_Y_INSET 40.0f
- @interface UIPopoverListView ()
- //- (void)defalutInit;
- - (void)fadeIn;
- - (void)fadeOut;
- @end
- @implementation UIPopoverListView
- NSString *popoverDateStr;
- //view
- UIView *popDetailView;
- @synthesize datasource = _datasource;
- @synthesize delegate = _delegate;
- @synthesize listView = _listView;
- @synthesize datePickerView=_datePickerView;
- - (id)initWithFrame:(CGRect)frame isDateView:(BOOL)isDateView
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self defalutInit:isDateView];
- }
- return self;
- }
- - (id)initWithFrame:(CGRect)frame forTextView:(BOOL)isTextView
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self textViewDefalutInit:isTextView];
- }
- return self;
- }
- - (id)initWithFrame:(CGRect)frame forMapView:(NSString *)webViewLink
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self webViewDefalutInit:webViewLink];
- }
- return self;
- }
- - (id)initWithFrame:(CGRect)frame imageName:(NSString*)name
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self imageViewDefalutInit:name];
- }
- return self;
- }
- - (void)imageViewDefalutInit:(NSString*)name
- {
- self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
- self.layer.borderWidth = 1.0f;
- self.layer.cornerRadius = 10.0f;
- self.clipsToBounds = TRUE;
-
- _titleView = [[UILabel alloc] initWithFrame:CGRectZero];
- _titleView.font = FONT_SIZE(15);
- _titleView.backgroundColor = HexColor(@"#4388B7");
- _titleView.textAlignment = NSTextAlignmentCenter;
- _titleView.textColor = [UIColor whiteColor];
- CGFloat xWidth = self.bounds.size.width;
- _titleView.lineBreakMode = NSLineBreakByCharWrapping;
- _titleView.frame = CGRectMake(0, 0, xWidth, 32.0f);
- [self addSubview:_titleView];
-
- UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 32.0f, self.bounds.size.width, self.bounds.size.height-32.0f)];
- view.backgroundColor=[UIColor whiteColor];
- [self addSubview:view];
- //UIImageView
- UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150,150)];
- imageView.center=CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2+17);
- imageView.image=[UIImage imageNamed:name];
- [self addSubview:imageView];
-
-
-
-
- _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.8];
-
-
- if (self.isCannotClickBackGround) {
- [_overlayView addTarget:self
- action:@selector(dismiss)
- forControlEvents:UIControlEventTouchUpInside];
-
- }
-
- }
- - (void)webViewDefalutInit:(NSString *)webViewLink
- {
-
- self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
- self.layer.borderWidth = 1.0f;
- self.layer.cornerRadius = 10.0f;
- self.clipsToBounds = TRUE;
-
- _titleView = [[UILabel alloc] initWithFrame:CGRectZero];
- _titleView.font = FONT_SIZE(15);
- _titleView.backgroundColor = HexColor(@"#4388B7");
- _titleView.textAlignment = NSTextAlignmentCenter;
- _titleView.textColor = [UIColor whiteColor];
- CGFloat xWidth = self.bounds.size.width;
- _titleView.lineBreakMode = NSLineBreakByCharWrapping;
- _titleView.frame = CGRectMake(0, 0, xWidth, 32.0f);
- [self addSubview:_titleView];
- //view
- popDetailView=[[UIView alloc] initWithFrame:CGRectMake(0, 32.0f, self.bounds.size.width, self.bounds.size.height-32.0f)];
- popDetailView.backgroundColor=[UIColor whiteColor];
- [self addSubview:popDetailView];
- //textView
-
- _webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 32, self.bounds.size.width, self.bounds.size.height)];
- [self addSubview:_webView];
- _webView.delegate=self;
- _webView.scalesPageToFit=YES;
- NSMutableURLRequest *preRequest;
- DLog(@"popWebView=%@",webViewLink);
- preRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:webViewLink]];
- [_webView loadRequest:preRequest];
- _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.8
- ];
- if (self.isCannotClickBackGround) {
- [_overlayView addTarget:self
- action:@selector(dismiss)
- forControlEvents:UIControlEventTouchUpInside];
-
- }
- }
- - (void)textViewDefalutInit:(BOOL)isTextView
- {
- self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
- self.layer.borderWidth = 1.0f;
- self.layer.cornerRadius = 10.0f;
- self.clipsToBounds = TRUE;
-
- _titleView = [[UILabel alloc] initWithFrame:CGRectZero];
- _titleView.font = FONT_SIZE(15);
- _titleView.backgroundColor = HexColor(@"#4388B7");
- _titleView.textAlignment = NSTextAlignmentCenter;
- _titleView.textColor = [UIColor whiteColor];
- CGFloat xWidth = self.bounds.size.width;
- _titleView.lineBreakMode = NSLineBreakByCharWrapping;
- _titleView.frame = CGRectMake(0, 0, xWidth, 32.0f);
- [self addSubview:_titleView];
- //view
- popDetailView=[[UIView alloc] initWithFrame:CGRectMake(0, 32.0f, self.bounds.size.width, self.bounds.size.height-32.0f)];
- popDetailView.backgroundColor=[UIColor whiteColor];
- [self addSubview:popDetailView];
- //textView
-
- self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 32.0f, self.bounds.size.width, self.bounds.size.height-32.0f)];//初始化大小并自动释放
-
- self.textView.textColor = [UIColor blackColor];//设置textview里面的字体颜色
-
- self.textView.font = [UIFont fontWithName:@"Arial" size:15.0];//设置字体名字和字体大小
-
- //self.textView.delegate = self;//设置它的委托方法
-
- self.textView.backgroundColor = [UIColor whiteColor];//设置它的背景颜色
-
- //self.textView.text = @"请在此处输入:";//设置它显示的内容
-
- self.textView.returnKeyType = UIReturnKeyDefault;//返回键的类型
-
- self.textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
-
- self.textView.scrollEnabled = YES;//是否可以拖动
-
- UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
- label.text=@"请在此处输入:";
- label.tag=1;
- [self.textView addSubview:label];
- [self addSubview:self.textView];
-
-
-
- _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.8
- ];
- if (self.isCannotClickBackGround) {
- [_overlayView addTarget:self
- action:@selector(dismiss)
- forControlEvents:UIControlEventTouchUpInside];
-
- }
- }
- - (void)defalutInit:(BOOL)isDateView
- {
- self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
- self.layer.borderWidth = 1.0f;
- self.layer.cornerRadius = 10.0f;
- self.clipsToBounds = TRUE;
-
- _titleView = [[UILabel alloc] initWithFrame:CGRectZero];
- _titleView.font = FONT_SIZE(15);
- _titleView.backgroundColor = HexColor(@"#4388B7");
- _titleView.textAlignment = NSTextAlignmentCenter;
- _titleView.textColor = [UIColor whiteColor];
- CGFloat xWidth = self.bounds.size.width;
- _titleView.lineBreakMode = NSLineBreakByCharWrapping;
- _titleView.frame = CGRectMake(0, 0, xWidth, 32.0f);
- [self addSubview:_titleView];
-
- //tableview
- //////////////////////////////////////
-
- if (isDateView) {
- UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 32, xWidth , self.bounds.size.height-32.0f)];
- view.backgroundColor=[UIColor whiteColor];
- [self addSubview:view];
-
- CGRect datePickerCR = CGRectMake(0, 32.0f, xWidth, self.bounds.size.height-32.0f);
- _datePickerView=[[UIDatePicker alloc] initWithFrame:datePickerCR];
- [_datePickerView addTarget:self action:@selector(dateAction:) forControlEvents:UIControlEventValueChanged];
- //设置中文区域化
- NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
- _datePickerView.locale=locale;
- //[_datePickerView selectRow:0 inComponent:0 animated:NO];
- [_datePickerView setDatePickerMode:UIDatePickerModeDate];
- [self addSubview:_datePickerView];
-
- //加一个按钮
- CGRect frame1 =CGRectMake(self.bounds.size.width/2-30, self.bounds.size.height-40.0f, 60, 30.0f);
- UIImage *buttonImageNomal=[UIImage imageNamed:@"changBlue.png"];
- UIImage *stretchableButtonImageNomal=[buttonImageNomal stretchableImageWithLeftCapWidth:12 topCapHeight:1];
- UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- button1.frame = frame1;
- [button1 setTitle:@"确定" forState: UIControlStateNormal];
- button1.backgroundColor = [UIColor clearColor];
- button1.tag = 2001;
- [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [button1 addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
- [button1 setBackgroundImage:stretchableButtonImageNomal forState:UIControlStateNormal];
- [self addSubview:button1];
-
-
- }else
- {
- CGRect tableFrame = CGRectMake(0, 32.0f, xWidth, self.bounds.size.height-32.0f);
- _listView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
- _listView.dataSource = self;
- _listView.delegate = self;
- [self addSubview:_listView];
- }
-
- _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
- if (self.isCannotClickBackGround) {
- [_overlayView addTarget:self
- action:@selector(dismiss)
- forControlEvents:UIControlEventTouchUpInside];
- }
-
- }
- -(void)setIsCannotClickBackGround:(BOOL)isCannotClickBackGround{
-
- if (isCannotClickBackGround) {
-
- [_overlayView addTarget:self
- action:@selector(dismiss)
- forControlEvents:UIControlEventTouchUpInside];
-
- }
-
- }
- #pragma mark -UIDatePicker
- //日期确定按钮
- BOOL isfirstOpen=YES;
- -(void)dateAction:(id)sender{
- UIDatePicker *control=(UIDatePicker *)sender;
- NSDate* date=control.date;
-
- NSDateFormatter * df2 = [[NSDateFormatter alloc] init];
-
- [df2 setDateFormat:@"yyyy-MM-dd"];
-
- NSString * str1 = [df2 stringFromDate:date];
- popoverDateStr=str1;
- [self.delegate setDateText:popoverDateStr];
- isfirstOpen=NO;
- DLog(@"date=%@",str1);
- }
- -(void)buttonClicked1:(id)sender{
- // [self.delegate setDateText:popoverDateStr];
- if (isfirstOpen) {
- [self dateAction:nil];
- }
- if (self.dateBlock) {
- self.dateBlock();
- }
- [self dismiss];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- if (self.datasource&&
- [self.datasource respondsToSelector:@selector(numberOfSectionsInUIPopoverListView:)]) {
- return [self.datasource numberOfSectionsInUIPopoverListView:self];
- }
- return 4;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- if(self.datasource &&
- [self.datasource respondsToSelector:@selector(popoverListView:numberOfRowsInSection:)])
- {
- return [self.datasource popoverListView:self numberOfRowsInSection:section];
- }
-
- return 0;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if(self.datasource &&
- [self.datasource respondsToSelector:@selector(popoverListView:cellForIndexPath:)])
- {
- return [self.datasource popoverListView:self cellForIndexPath:indexPath];
- }
- return nil;
- }
- -(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
- {
- //return [stories objectAtIndex:section];
- if(self.datasource &&
- [self.datasource respondsToSelector:@selector(popoverListView:titleForHeaderInSection:)])
- {
- return [self.datasource popoverListView:self titleForHeaderInSection:section];
- }
- return @"";
- }
- #pragma mark - UITableViewDelegate
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if(self.delegate &&
- [self.delegate respondsToSelector:@selector(popoverListView:heightForRowAtIndexPath:)])
- {
- return [self.delegate popoverListView:self heightForRowAtIndexPath:indexPath];
- }
-
- return 0.0f;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if(self.delegate &&
- [self.delegate respondsToSelector:@selector(popoverListView:didSelectIndexPath:)])
- {
- [self.delegate popoverListView:self didSelectIndexPath:indexPath];
- }
-
- [self dismiss];
- }
- #pragma mark --- 显示完整分割线
- -(void)viewDidLayoutSubviews {
-
- if ([_listView respondsToSelector:@selector(setSeparatorInset:)]) {
- [_listView setSeparatorInset:UIEdgeInsetsZero];
- }
- if ([_listView respondsToSelector:@selector(setLayoutMargins:)]) {
- [_listView setLayoutMargins:UIEdgeInsetsZero];
- }
- }
- #pragma mark - animations
- - (void)fadeIn
- {
- self.transform = CGAffineTransformMakeScale(1.3, 1.3);
- self.alpha = 0;
- [UIView animateWithDuration:.35 animations:^{
- self.alpha = 1;
- self.transform = CGAffineTransformMakeScale(1, 1);
- }];
-
- }
- - (void)fadeOut
- {
- [UIView animateWithDuration:.35 animations:^{
- self.transform = CGAffineTransformMakeScale(1.3, 1.3);
- self.alpha = 0.0;
- } completion:^(BOOL finished) {
- if (finished) {
- [self->_overlayView removeFromSuperview];
- [self removeFromSuperview];
- }
- }];
- }
- - (void)setTitle:(NSString *)title
- {
- _titleView.text = title;
- }
- - (void)show
- {
- UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
- [keywindow addSubview:_overlayView];
- [keywindow addSubview:self];
-
- self.center = CGPointMake(keywindow.bounds.size.width/2.0f,self.frame.origin.y+self.frame.size.height/2);
- // keywindow.bounds.size.height/2.0f);
- [self fadeIn];
- }
- - (void)dismiss
- {
-
- [self fadeOut];
- isfirstOpen=YES;
- if (self.BackBlock) {
- self.BackBlock();
- }
- }
- #define mark - UITouch
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- // tell the delegate the cancellation
- if (self.delegate && [self.delegate respondsToSelector:@selector(popoverListViewCancel:)]) {
- [self.delegate popoverListViewCancel:self];
- }
- DLog(@"Cancel!!!!!!!");
- // dismiss self
- [self dismiss];
- }
- @end
|