一、在滚动页面(tableview/collectionview)添加协议方法
NSNotification *_notice;(定义属性)
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
contentOffsetY=scrollView.contentOffset.y;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
newContentOffsetY = collectionViewHome.contentOffset.y;
if (newContentOffsetY > oldContentOffsetY && oldContentOffsetY > contentOffsetY) { // 向上滚动
NSLog(@"up");
//发送通知===上滑滚动=====
NSMutableDictionary * dic1=[[NSMutableDictionary alloc]initWithCapacity:72];
[dic1 setValue:@"itisup" forKey:@"scrollup"];
_notice=[NSNotification notificationWithName:@"pagescrollupdown" object:@"pagescroll" userInfo:dic1];
[[NSNotificationCenter defaultCenter] postNotification:_notice];
collectionViewHome.frame=CGRectMake(0, 0, screen_width,self.view.frame.size.height-64-49);
} else if (newContentOffsetY < oldContentOffsetY && oldContentOffsetY < contentOffsetY) {// 向下滚动
NSLog(@"down");
//发送通知===下滚通知====
NSMutableDictionary * dic1=[[NSMutableDictionary alloc]initWithCapacity:72];
[dic1 setValue:@"itisdown" forKey:@"scrolldown"];
_notice=[NSNotification notificationWithName:@"pagescrollupdown" object:@"pagescroll" userInfo:dic1];
[[NSNotificationCenter defaultCenter] postNotification:_notice];
} else {
NSLog(@"dragging");
}
if (scrollView.dragging) { // 拖拽
NSLog(@"contentOffsetY: %f", contentOffsetY);
NSLog(@"newContentOffsetY: %f", collectionViewHome.contentOffset.y);
if ((scrollView.contentOffset.y - contentOffsetY) >5.0f) { // 向上拖拽
} else if ((contentOffsetY - scrollView.contentOffset.y) > 5.0f) { // 向下拖拽
} else {
}
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
oldContentOffsetY=scrollView.contentOffset.y;
}
二、接收通知页面
//-======定义通知接收中心,一般写在viewdidload中====
NSNotificationCenter * center1=[NSNotificationCenter defaultCenter];
[center1 addObserver:self selector:@selector(scrollupNotice:) name:@"pagescrollupdown" object:@"pagescroll"];
//====== 在接受通知的地方,写接收方法 =======
-(void)scrollupNotice:(NSNotification *)sender{
NSLog(@"fir:%@",sender);
if ([sender.userInfo[@"scrollup"] isEqualToString:@"itisup"]) {
NSLog(@"收到上滑通知");
[UIView animateWithDuration:0.2 animations:^{
searchBackView.hidden=YES;
CGRect Rect = CGRectMake(0, 0, SC_WIDTH, SC_HIGHT+49);
LyMenu.frame=Rect;
}];
}else if ([sender.userInfo[@"scrolldown"] isEqualToString:@"itisdown"]){
[UIView animateWithDuration:0.2 animations:^{
searchBackView.hidden=NO;
CGRect Rect = CGRectMake(0, searchBackView.frame.size.height, SC_WIDTH, SC_HIGHT);
LyMenu.frame=Rect;
}];
}
}
//=====释放通知=====
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"pagescrollupdown" object:@"pagescroll"];
}