我想实现的效果:
在 uitableview的中的某一行选中后,弹出pickerview,进行选择,当点击其他行时退出。
注意 uitableview的某一行 不一定是最后一行,所以不能简单的 将uitableview加到mainview后,将pickerview也加到mianview,只要控制位置。
我的想法:
因为有可能在中间一行,所以我想将pickerview 加到uitableview的某一行,并且要设置这个一行的高度,并且根据是否需要显示pickerview动态显示这一行。
后来搜到以下资料,好像可以实现类似效果,所以就采用以下替换方法。
http://stackoverflow.com/questions/1262574/add-uipickerview-a-button-in-action-sheet-how
其中,UIToolBar的对齐方式是左对齐的,如果想要右对齐,或居中对齐,可以采用填充的方式,具体如下 见其中的flexspace
pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
[barItems addObject:doneBtn];
[pickerDateToolbar setItems:barItems animated:YES];
1.//实现如下,有个问题,设置uiactionsheet的高度总是不对。好像你怎么设 他都没反应,或者变化很大。算了,不适合我这里使用,放弃。
wordThinkActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] ;
//wordThinkPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 40.0/*toolbar*/, 320, 60)];
wordThinkPickerView = [[UIPickerView alloc] init];
wordThinkPickerView.showsSelectionIndicator = YES; // note this is default to NO
// this view controller is the data source and delegate
wordThinkPickerView.delegate = self;
wordThinkPickerView.dataSource = self;
UIToolbar* pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
//free space
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexibleSpace];
[flexibleSpace release] ;
//done button
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(wordThinkPickerDoneClick)];
[barItems addObject:doneBtn];
[doneBtn release] ;
[pickerDateToolbar setItems:barItems animated:YES];
[wordThinkActionSheet addSubview:pickerDateToolbar];
[wordThinkActionSheet addSubview:wordThinkPickerView];
[wordThinkActionSheet showInView:self.view];
//[wordThinkActionSheet showFromRect:CGRectMake(0,300,320, 180) inView:self.view animated:YES];
[wordThinkActionSheet setBounds:CGRectMake(0,100,320, 300)];
[wordThinkPickerView setFrame:CGRectMake(0.0, 40.0/*toolbar*/, 320, 260)];
[barItems release] ;
[pickerDateToolbar release] ;
2.自己实现uipickerview从低部进入的动画效果。并且需要实现点击背景 消除
uipickerview。
动画
You will avoid the display problem and get a much better looking result if you simply put the UIDatePicker in a new view and implement the slide-up-from-the-bottom behavior yourself. It's not that hard.
- Create a UIView containing a date picker and a toolbar with a Done button on it. (In code or use Interface Builder, doesn't matter.)
- Add the popup view as a subview of your main view.
- Set the popup view's frame so that it is off the bottom of the screen:
frame.origin.y = CGRectGetMaxY(mainView.bounds)
- Call
[UIView beginAnimations:nil context:nil]
- Set the frame to where it should be:
frame.origin.y -= CGRectGetHeight(popupView.bounds)
- Call
[UIView commitAnimations]
(Note that the frame setting stuff is pseudocode.)
That's it. Do it in reverse when you need to dismiss the view. I think it will be worth the trouble; sticking a date picker in a UIActionSheet looks incredibly tacky.
消除
uipickerview主要思想,给你的tableview 加 一个透明的UIVIew的派生类覆盖在talbeview上 然后在这个UIview的 touchBegan里写一个postNotification ,然后在原来的那个类处理 notification 事件,消除uipickerview即可。NSnotification 使用见http://stackoverflow.com/questions/2191594/how-to-send-and-receive-message-through-nsnotificationcenter-in-objective-cuiview派生类实现#import <UIKit/UIKit.h> @interface BackgroundTapView : UIView{ } @end#import "BackgroundTapView.h" @implementation BackgroundTapView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[NSNotificationCenter defaultCenter]postNotificationName:@"BackgroundTapNotification" object:self ] ; [super touchesBegan:touches withEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { [super touchesEnded:touches withEvent:event]; } - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { [super touchesCancelled:touches withEvent:event]; } @end