Objective c 滚动时触摸UITableView背景(类似复活节彩蛋)

Objective c 滚动时触摸UITableView背景(类似复活节彩蛋),objective-c,uitableview,Objective C,Uitableview,我需要在表视图的底部做一个隐藏按钮,而不是在单元格中,在表视图本身的背景中 我在一个游戏《我的水在哪里》中看到了类似的东西,如果你滚动出边界,你可以找到一个隐藏的按钮 首先,我创建了一个简单的按钮,并将其放置在表视图的底部 self.tableView.backgroundColor = [UIColor purpleColor]; //just to see better UIButton *venom = [[UIButton alloc] initWithFrame:CGRectMake(

我需要在表视图的底部做一个隐藏按钮,而不是在单元格中,在表视图本身的背景中

我在一个游戏《我的水在哪里》中看到了类似的东西,如果你滚动出边界,你可以找到一个隐藏的按钮

首先,我创建了一个简单的按钮,并将其放置在表视图的底部

self.tableView.backgroundColor = [UIColor purpleColor]; //just to see better
UIButton *venom = [[UIButton alloc] initWithFrame:CGRectMake(50.0, self.tableView.contentSize.height+80.0, 100.0, 40.0)];
venom.backgroundColor = [UIColor redColor];
[venom addTarget:self action:@selector(venomAction) forControlEvents:UIControlEventTouchDown];
[self.tableView addSubview:venom];
由于self.tableView.contentSize.height+80.0,我需要滚动出表格视图的边界才能看到按钮,如下所示:

结果是正确的,我想这是一种隐藏,但问题是,我不能点击按钮,看到按钮,我需要滚动,我不能多点触摸


有人能帮我吗?或者将我指向正确的方向

我会使按钮不是tableView的子视图,而是tableView上方的同级视图。 例如,您的代码可能如下所示

- (void)viewDidLoad {
  [super viewDidLoad];

  // Just creating a simple UITableView
  UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
  tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  tableView.dataSource = self;
  tableView.delegate = self;
  self.tableView = tableView;
  [self.view addSubview:tableView];

  // Creating the Easter Egg Button
  UIButton *easterEggButton = [UIButton buttonWithType:UIButtonTypeSystem];
  [easterEggButton setTitle:@"Easter Egg Button" forState:UIControlStateNormal];
  [easterEggButton addTarget:self
                      action:@selector(easterEggButtonPressed)
            forControlEvents:UIControlEventTouchUpInside];
  self.easterEggButton = easterEggButton;

  // NOTE that we add the Easter Egg Button to self.view over the Table View, not into Table View
  [self.view addSubview:easterEggButton];
}
因此,我们将按钮放置在tableView上。这将使其与tableView的手势识别器分离。 现在需要一些代码来调整按钮的框架,使其位于tableView的最后一个单元格下。它可能看起来像这样:

- (void)updateButtonFrame {
  UIButton *button = self.easterEggButton;

  // Just the easiest way calculate the width and height of the button depending on it's content
  [button sizeToFit];
  CGRect frame = button.bounds;

  // Now, we want to place the button under all the cells
  frame.origin.y = self.tableView.contentSize.height;
  frame.origin.y += 60.0f; // add some padding to place it even lower

  /* We have calculated button's frame in tableView's coordinate space, so we need to convert it to
   * button superview's coordinate space. */
  frame = [self.tableView convertRect:frame toView:button.superview];

  // And finally apply the frame to the button
  button.frame = frame;
}
当然,您需要在适当的时候调用这个布局方法,也就是说,每当tableView因为contentSize可能更改而重新加载其数据时,以及每当tableView滚动时

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  /* Button resides in self.view and is unaware of Table View scrolling, so we need to update
   * it's frame whenever Table View's content offset changes */
  [self updateButtonFrame];
}
如果tableView的单元格高度取决于屏幕方向等,为了安全起见,您也可以将其称为viewDidLayoutSubviews

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];
  // It's safe to recalculate button's frame whenever self.view layout updates
  [self updateButtonFrame];
}
您可以看看这个示例实现

简单而优雅!非常感谢你!!