實現這一功能,只能經過UIScrollView的嵌套或者UIView中添加UIScrollView方式(我能想到的,有更好的作法,能夠留言指出)git
但無論採用哪一種方案,有一個重要的問題須要處理:事件衝突github
在通常的狀況下,只有UIScrollView及其子類才具有滾動的功能,無論是UIScrollView的嵌套仍是在普通的UIView中添加UIScrollView,都會出現衝突bash
採用UIView中添加UICollectionView,而後給UICollectionView添加拖動手勢,
經過位置、拖動方向,攔截事件來肯定響應的對象。
複製代碼
1.這個是UIView關於手勢事件的拓展方法,返回值爲NO時,表示不觸發手勢事件,該方法在此處運用時,即禁掉自定義添加的拖動手勢,響應UICollecionView的滾動手勢,咱們能夠在這個方法中獲取到view的當前位置,以及手勢的方向,根據這兩個因素,就能夠決定是否響應事件了
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer *recognizer = (UIPanGestureRecognizer *)gestureRecognizer;
if ([self.delegate respondsToSelector:@selector(fetchContainerViewWithStartY)]) {
/*經過代理獲取view的當前位置*/
CGFloat startY = [self.baseCollectionViewdelegate fetchContainerViewWithStartY];
CGPoint point = [recognizer translationInView:recognizer.view];//處理方向
/*
1.外層view是否在最頂部即frame的y值是否爲0(在中止時,y值只有三種狀況:0, 150, ScreenHeight-49)
2.scrollview的偏移值 ( contentOffset.y < 0 偏下 >0 偏上)
3.滑動方向:向上仍是向下 ( point.y > 0:向下, point.y > 0:向上)
*/
if (startY <= 20) {
if (point.y > 0) {//向下
if (self.contentOffset.y > 0) {// <0 偏下(目前的設置,不可能出現) >0 偏上
return YES;
} else {
return NO;
}
} else {
return YES;
}
} else {
return NO;
}
}
}
return YES;
}
2.是否支持多種手勢事件共存,這個也是解決問題的關鍵,這裏須要返回YES,容許支持,咱們對UICollectionView添加了自定義的拖動手勢以及UICollectionView自己自帶了系統的事件
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
複製代碼
- (void)moveView:(UIPanGestureRecognizer *)recognizer {
NSLog(@"手勢滑動 > recognizer.state : %ld", recognizer.state);
if (CGRectGetMinY(self.frame) > 20) {
CGPoint location = [recognizer translationInView:self.superview];
CGFloat y = location.y + CGRectGetMinY(self.frame);
if (recognizer.state == UIGestureRecognizerStateBegan) {
self.startMoveViewFrameY = CGRectGetMinY(self.frame);
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
if (y < 20) {
y = 20;
} else if (y > SCREEN_HEIGHT - 49.f) {
y = SCREEN_HEIGHT - 49.f;
}
self.frame = CGRectMake(0, y, SCREEN_WIDTH, SCREEN_HEIGHT - 69.f);
if ([self.delegate respondsToSelector:@selector(scrollWithY:panDirection:animations:)]) {
//回調處理,拖動的小圖標
[self.delegate scrollWithY:y panDirection:HZPanDirectionNone animations:NO];
}
} else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
CGPoint locan = [recognizer translationInView:self.collectionView];
HZPanDirection panDirection = HZPanDirectionNone;
if (self.startMoveViewFrameY < CGRectGetMinY(self.frame)) {//向下
y = SCREEN_HEIGHT - 49.f;
panDirection = HZPanDirectionDown;
} else {
panDirection = HZPanDirectionUp;
if (y < 150) {
y = 20;
} else {
y = 150.f;
}
}
[UIView animateWithDuration:1.f animations:^{
self.frame = CGRectMake(0, y, SCREEN_WIDTH, SCREEN_HEIGHT - 69.f);
} completion:^(BOOL finished) {
}];
if ([self.delegate respondsToSelector:@selector(scrollWithY:panDirection:animations:)]) {
//回調處理,拖動的小圖標
[self.delegate scrollWithY:y panDirection:panDirection animations:YES];
}
}
} else if (CGRectGetMinY(self.frame) == 20) {
CGPoint point = [recognizer translationInView:recognizer.view];//處理方向
if (point.y > 0 && self.collectionView.contentOffset.y <= 0) {//向下
[UIView animateWithDuration:1.f animations:^{
self.frame = CGRectMake(0, SCREEN_HEIGHT - 49.f, SCREEN_WIDTH, SCREEN_HEIGHT - 69.f);
} completion:^(BOOL finished) {
}];
if ([self.delegate respondsToSelector:@selector(scrollWithY:panDirection:animations:)]) {
//回調處理,拖動的小圖標
[self.delegate scrollWithY:SCREEN_HEIGHT - 49.f panDirection:HZPanDirectionDown animations:YES];
}
}
}
[recognizer setTranslation:CGPointZero inView:self.superview];
}
複製代碼
這裏是闡述比較核心的幾個方法,具體的使用,能夠下載demo : 傳送門模塊化