Recently I was asked about a function, which is similar to meituan select goods interface, slide up from the button to submit an order, tableView can respond, slide up, click the button, the button is not affected, do not understand friends can open Meituan look, slide up from the button, tableView can respond.
At first, the idea was to use button’s addTarget method to give the tableView an upward offset when dragOut, but the sliding was too rigid and not very friendly. Finally, I thought of using gesture to pass hitTest, but there was an extra button in the scene, which was also abandoned. Finally, Think of buttons and click areas to solve this problem.
Ideas: Make them all superView self.view, and make the tableView tableFooterView the same width and height as the button, so that the button doesn’t block the contents of the tableView, and when the button slides after the button closes the interaction, Add the click gesture to self.view and set the effective range of click to the button’s area:
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate> { UITableView *tableView; UIButton *btn; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain]; tableView.delegate = self; tableView.dataSource = self; [self.view addSubview:tableView]; tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)]; btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.userInteractionEnabled = NO; btn.frame = CGRectMake(0, self.view.bounds.size.height - 50, self.view.bounds.size.width, 50); [btn setTitle:@"Centain" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; btn.backgroundColor = [UIColor orangeColor]; [self.view addSubview:btn]; UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnAction:)]; ges.delegate = self; [self.view addGestureRecognizer:ges]; } #pragma mark - btnAction - (void)btnAction:(UITapGestureRecognizer *)ges { UIAlertController *alert = [UIAlertController alertControllerWithTitle: @ "prompt message:" @ "click the button" preferredStyle: UIAlertControllerStyleAlert]; UIAlertAction * defaultAction = [UIAlertAction actionWithTitle: @ "sure" style: UIAlertActionStyleDefault handler: nil]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { CGPoint point = [gestureRecognizer locationInView:self.view]; if (point.y < self.view.bounds.size.height - 50) { return NO; } return YES; } #pragma mark - UITableViewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 30; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (! cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } cell.textLabel.text = [NSString stringWithFormat:@"This is the row at index %ld!!!", indexPath.row]; return cell; }Copy the code
The code is relatively simple, I will not put git link, put a effect picture at the end.