1.想實現的效果:
浏覽文章的時候,當向下滑動時候,navigationBar 和 toolbar 隱藏 , 當到結尾時候再向上滑動,navigationBar 和 toolbar 重新顯示出來。
2.思路:
首先,這裡用來顯示文章的是webview ,我們都知道webview中包含scrollview,這樣就好辦了,我們利用scrollview來實現即可。
代碼如下:
復制代碼
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int currentPostion = scrollView.contentOffset.y;
if (currentPostion - lastPostion > kSlide && currentPostion > 0) {
lastPostion = currentPostion;
//重設frame
[UIView animateWithDuration:kAnimationTime animations:^{
CGRect rc = self.navigationController.navigationBar.frame;
self.navigationController.navigationBar.frame = CGRectMake(0, -CGRectGetHeight(rc), CGRectGetWidth(rc), CGRectGetHeight(rc));
rc= self.toolbar.frame;
self.toolbar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, CGRectGetWidth(rc), CGRectGetHeight(rc));
self.webView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}];
}
else if (lastPostion - currentPostion > kSlide && (currentPostion <= scrollView.contentSize.height-scrollView.bounds.size.height-kSlide))
{
lastPostion = currentPostion;
//重設frame
[UIView animateWithDuration:kAnimationTime animations:^{
CGRect rc = self.navigationController.navigationBar.frame;
self.navigationController.navigationBar.frame = CGRectMake(0, 0, CGRectGetWidth(rc), CGRectGetHeight(rc));
rc= self.toolbar.frame;
self.toolbar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - CGRectGetHeight(rc), CGRectGetWidth(rc), CGRectGetHeight(rc));
self.webView.frame = CGRectMake(0, CGRectGetHeight(self.navigationController.navigationBar.frame), [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - CGRectGetHeight(self.navigationController.navigationBar.frame) - CGRectGetHeight(rc));
}];
}
}