Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ios bugfix picker reuse #1761

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 51 additions & 76 deletions ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

@interface WXPickerModule()

//view
//picker
@property(nonatomic,strong)UIPickerView *picker;
@property(nonatomic,strong)UIView *backgroudView;
@property(nonatomic,strong)UIView *backgroundView;
@property(nonatomic,strong)UIView *pickerView;

//data
@property(nonatomic,copy)NSArray *items;
@property(nonatomic)BOOL isAnimating;
Expand All @@ -42,17 +42,6 @@ @implementation WXPickerModule
WX_EXPORT_METHOD(@selector(pickDate:callback:))
WX_EXPORT_METHOD(@selector(pickTime:callback:))

#pragma mark - private method
-(void)resetPickerView
{
if (self.picker) {
[self.picker removeFromSuperview];
}
if (self.pickerView) {
[self.pickerView removeFromSuperview];
}
}

#pragma mark -
#pragma mark Single Picker
-(void)pick:(NSDictionary *)options callback:(WXModuleCallback)callback
Expand Down Expand Up @@ -91,15 +80,15 @@ -(void)show
{
[[[UIApplication sharedApplication] keyWindow] endEditing:YES]; //hide keyboard
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:self.backgroudView];
[window addSubview:self.backgroundView];
if (self.isAnimating) {
return;
}
self.isAnimating = YES;
self.backgroudView.hidden = NO;
self.backgroundView.hidden = NO;
[UIView animateWithDuration:0.35f animations:^{
self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - WXPickerHeight, [UIScreen mainScreen].bounds.size.width, WXPickerHeight);
self.backgroudView.alpha = 1;
self.backgroundView.alpha = 1;
} completion:^(BOOL finished) {
self.isAnimating = NO;
}];
Expand All @@ -113,11 +102,11 @@ -(void)hide
self.isAnimating = YES;
[UIView animateWithDuration:0.35f animations:^{
self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, WXPickerHeight);
self.backgroudView.alpha = 0;
self.backgroundView.alpha = 0;
} completion:^(BOOL finished) {
self.backgroudView.hidden = YES;
self.backgroundView.hidden = YES;
self.isAnimating = NO;
[self.backgroudView removeFromSuperview];
[self.backgroundView removeFromSuperview];
}];
}

Expand All @@ -140,33 +129,26 @@ -(void)done:(id)sender

-(void)configPickerView
{
if (!self.backgroudView) {
self.backgroudView = [self createBackgroudView];
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
[self.backgroudView addGestureRecognizer:tapGesture];
}
if (!self.pickerView) {
self.pickerView = [self createPickerView];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, WXPickerToolBarHeight)];
[toolBar setBackgroundColor:[UIColor whiteColor]];
UIBarButtonItem* noSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
noSpace.width=10;
UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
[toolBar setItems:[NSArray arrayWithObjects:noSpace,cancelBtn,flexSpace,doneBtn,noSpace, nil]];
[self.pickerView addSubview:toolBar];
}
if (!self.picker) {
self.picker = [[UIPickerView alloc]init];
self.picker.delegate = self;
CGRect pickerFrame = CGRectMake(0, WXPickerToolBarHeight, [UIScreen mainScreen].bounds.size.width, WXPickerHeight-WXPickerToolBarHeight);
self.picker.backgroundColor = [UIColor whiteColor];
self.picker.frame = pickerFrame;
}
[self resetPickerView];
self.backgroundView = [self createbackgroundView];
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
[self.backgroundView addGestureRecognizer:tapGesture];
self.pickerView = [self createPickerView];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, WXPickerToolBarHeight)];
[toolBar setBackgroundColor:[UIColor whiteColor]];
UIBarButtonItem* noSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
noSpace.width=10;
UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
[toolBar setItems:[NSArray arrayWithObjects:noSpace,cancelBtn,flexSpace,doneBtn,noSpace, nil]];
[self.pickerView addSubview:toolBar];
self.picker = [[UIPickerView alloc]init];
self.picker.delegate = self;
CGRect pickerFrame = CGRectMake(0, WXPickerToolBarHeight, [UIScreen mainScreen].bounds.size.width, WXPickerHeight-WXPickerToolBarHeight);
self.picker.backgroundColor = [UIColor whiteColor];
self.picker.frame = pickerFrame;
[self.pickerView addSubview:self.picker];
[self.backgroudView addSubview:self.pickerView];
[self.backgroundView addSubview:self.pickerView];
}

-(UIView *)createPickerView
Expand All @@ -177,7 +159,7 @@ -(UIView *)createPickerView
return view;
}

-(UIView *)createBackgroudView
-(UIView *)createbackgroundView
{
UIView *view = [UIView new];
view.frame = [UIScreen mainScreen].bounds;
Expand Down Expand Up @@ -234,9 +216,7 @@ -(void)datepick:(NSDictionary *)options callback:(WXModuleCallback)callback
- (void)createDatePicker:(NSDictionary *)options callback:(WXModuleCallback)callback
{
self.callback = callback;
if (!self.datePicker) {
self.datePicker = [[UIDatePicker alloc]init];
}
self.datePicker = [[UIDatePicker alloc]init];
if (UIDatePickerModeDate == self.datePickerMode) {
self.datePicker.datePickerMode = UIDatePickerModeDate;
NSString *value = [WXConvert NSString:options[@"value"]];
Expand Down Expand Up @@ -276,44 +256,39 @@ - (void)createDatePicker:(NSDictionary *)options callback:(WXModuleCallback)call

-(void)configDatePickerView
{
if (!self.backgroudView) {
self.backgroudView = [self createBackgroudView];
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
[self.backgroudView addGestureRecognizer:tapGesture];
}
if (!self.pickerView) {
self.pickerView = [self createPickerView];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, WXPickerToolBarHeight)];
[toolBar setBackgroundColor:[UIColor whiteColor]];
UIBarButtonItem* noSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
noSpace.width=10;
UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneDatePicker:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects:noSpace,cancelBtn,flexSpace,doneBtn,noSpace, nil]];
[self.pickerView addSubview:toolBar];
}
self.backgroundView = [self createbackgroundView];
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
[self.backgroundView addGestureRecognizer:tapGesture];
self.pickerView = [self createPickerView];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, WXPickerToolBarHeight)];
[toolBar setBackgroundColor:[UIColor whiteColor]];
UIBarButtonItem* noSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
noSpace.width=10;
UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneDatePicker:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects:noSpace,cancelBtn,flexSpace,doneBtn,noSpace, nil]];
[self.pickerView addSubview:toolBar];
CGRect pickerFrame = CGRectMake(0, WXPickerToolBarHeight, [UIScreen mainScreen].bounds.size.width, WXPickerHeight-WXPickerToolBarHeight);
self.datePicker.backgroundColor = [UIColor whiteColor];
self.datePicker.frame = pickerFrame;
[self resetPickerView];
self.datePicker.backgroundColor = [UIColor whiteColor];
[self.pickerView addSubview:self.datePicker];
[self.backgroudView addSubview:self.pickerView];
[self.backgroundView addSubview:self.pickerView];
}

-(void)showDatePicker
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:self.backgroudView];
[window addSubview:self.backgroundView];
if (self.isAnimating)
{
return;
}
self.isAnimating = YES;
self.backgroudView.hidden = NO;
self.backgroundView.hidden = NO;
[UIView animateWithDuration:0.35f animations:^{
self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - WXPickerHeight, [UIScreen mainScreen].bounds.size.width, WXPickerHeight);
self.backgroudView.alpha = 1;
self.backgroundView.alpha = 1;
} completion:^(BOOL finished) {
self.isAnimating = NO;
}];
Expand All @@ -327,11 +302,11 @@ -(void)hideDatePicker
self.isAnimating = YES;
[UIView animateWithDuration:0.35f animations:^{
self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, WXPickerHeight);
self.backgroudView.alpha = 0;
self.backgroundView.alpha = 0;
} completion:^(BOOL finished) {
self.backgroudView.hidden = YES;
self.backgroundView.hidden = YES;
self.isAnimating = NO;
[self.backgroudView removeFromSuperview];
[self.backgroundView removeFromSuperview];
}];
}

Expand Down