有这样一个需求如图所示:
绿色是navigation,高64,红色是一个搜索栏,高44,黄色是一个tableview
约束是红色top距离self.view的top 64 约束名称为:
self.searchBarTopConstraint
由于使用PureLayout,距离(64)可以用下面名称:
self.searchBarTopConstraint.constant
取到
黄色table的top距离红色bottom 0
现在要求:
1.黄色table上滑时红色随黄色上移直到完全被绿色遮盖。
2.黄色table下滑时红色随黄色下移直到回到状态1的位置。
注:
黄色下滑后在任何时候上滑皆会触发要求1
黄色上滑后在任何时候下滑皆会触发要求2
关键代码如下:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat newPoint = scrollView.contentOffset.y;
CGFloat changePoint = self.oldPoint - newPoint;
if (changePoint < 0) {
NSLog(@"上滑");
if ((self.stopScroll && self.scrollUp) || !self.stopScroll) {
self.scrollUp = YES;
if (self.searchBarTopConstraint.constant > 20) {
//self.searchBarTopConstraint.constant > 20 意思是当搜索框没有完全被上面高64的navigation盖住时(searchBar高44,所以64-44=20)
self.searchBarTopConstraint.constant += changePoint;
} else if (self.searchBarTopConstraint.constant <= 20) {
self.searchBarTopConstraint.constant = 20;
}
}
} else if (changePoint > 0){
NSLog(@"下滑");
if ((self.stopScroll && !self.scrollUp) || !self.stopScroll) {
self.scrollUp = NO;
if (self.searchBarTopConstraint.constant<64) {
//self.searchBarTopConstraint.constant < 64 意思是当搜索框没有完全脱离navigation时
self.searchBarTopConstraint.constant += changePoint;
} else if (self.searchBarTopConstraint.constant >= 64) {
self.searchBarTopConstraint.constant = 64;
}
}
}
self.oldPoint = newPoint;
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
self.stopScroll = YES;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.stopScroll = NO;
[self.searchController.searchBar endEditing:YES];
}
问题:在cell数量足够的情况下比较正常(但不太平滑),不过在cell数量不满一个屏幕时会造成无法滑动的情况,究其原因是在此情况下向上滑动后不知为何会立刻向下滑动,不知怎么解决,求赐教。
在
viewDidLoad
里设置:因为设置了content inset 的原因, 一开始的content offset y 是-44。