滚动tableview时编辑导航栏的alpha

Edit the alpha of navigation bar when scrolls tableview

我想问问是否可以在用户滚动表格视图时更改导航栏的 alpha 值。

我有算法,但我需要一些帮助来实时更改。

  /* This is the offset at the bottom of the scroll view. */
CGFloat totalScroll = scrollView.contentSize.height - scrollView.bounds.size.height;

/* This is the current offset. */
CGFloat offset = - scrollView.contentOffset.y;

/* This is the percentage of the current offset / bottom offset. */
CGFloat percentage = offset / totalScroll;

/* When percentage = 0, the alpha should be 1 so we should flip the percentage. */
scrollView.alpha = (1.f - percentage);

可能为时已晚,但为了将来参考,您可以这样做:

 func scrollViewDidScroll(scrollView: UIScrollView) {
     self.navigationController!.navigationBar.alpha = 1 - (self.tableView.contentOffset.y / (self.tableView.contentSize.height - self.tableView.frame.size.height));
 }

在这个delegate上你有scrollview或者tableView的contentOffset的值,你可以观察这个属性的值来做出你想要的行为。

希望对您有所帮助!