美文网首页
iOS UICollectionView左右滚动和上下滑动处理

iOS UICollectionView左右滚动和上下滑动处理

作者: 弹吉他的少年 | 来源:发表于2020-09-18 14:10 被阅读0次

前言

  • 最近做了一个这样子的需求,在CollectionView上面移动Item并且不影响正常CollectionView的左右滑动
  • 核心其实就是解决左右滚动和上下滑动冲突处理
  • TableView和CollectionView道理是一样的,可以采用同样的方式去处理

Demo地址

这里提供两种思路

第一种,在Item添加Pan手势,

大致思路,在Pan手势当中去做逻辑判断处理回调出去,然后改变CollectionView 的contentOffset

__block bool move = false;
__block CGPoint beginPoint = CGPointZero;
[self.scanImageView kj_AddGestureRecognizer:(KJGestureTypePan) block:^(UIView * _Nonnull view, UIGestureRecognizer * _Nonnull gesture) {
    CGPoint point = [gesture locationInView:view];
    if (gesture.state == UIGestureRecognizerStateBegan) {
        beginPoint = [gesture locationInView:view];
    }else if (point.y <= 5 && move == false) {
        move = true;
        CGPoint pt = [gesture locationInView:kKeyWindow];
        if (weakself.kMoveBlock) weakself.kMoveBlock(weakself,pt,0);
    }else if (move && point.y < 5 && gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint pt = [gesture locationInView:kKeyWindow];
        if (weakself.kMoveBlock) weakself.kMoveBlock(weakself,pt,1);
    }else if (gesture.state == UIGestureRecognizerStateRecognized) {
        move = false;
        beginPoint = CGPointZero;
        CGPoint pt = [gesture locationInView:kKeyWindow];
        if (weakself.kMoveBlock) weakself.kMoveBlock(weakself,pt,2);
    }else {
        point.x = beginPoint.x - point.x;
        point.y = beginPoint.y - point.y;
        if (weakself.kMoveTableBlock) weakself.kMoveTableBlock(weakself,point);
    }
}];

改变CollectionView的contentOffset

/// 拖动手势回调
cell.kMoveBlock = ^(UICollectionViewCell *item,CGPoint point,NSInteger move){
    if (weakSelf.matterImageMoveBlock) {
        NSIndexPath *idx = kIndexpathSubviewCollectionview(item, self.collectView);
        weakSelf.matterImageMoveBlock(move,point,idx.row-1);
    }
};
cell.kMoveTableBlock = ^(UICollectionViewCell *item,CGPoint point) {
    NSLog(@"---:%.2f,%.2f",weakSelf.collectView.contentOffset.x,point.x);
    if (abs(point.x) < 5) {
        return;
    }
    weakSelf.collectView.contentOffset = CGPointMake(point.x,0);
};
总结:这种方式缺点就是手动改变contentOffset排除点不完美的情况,会导致左右拖动的时候出现CollectionView跳动的情况

第二种,获取CollectionView的Touch事件

大致思路,方法交换获取到Touch事件,然后以回调的方式传递出来

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self kj_swizzleMethod:@selector(touchesBegan:withEvent:) Method:@selector(kj_touchesBegan:withEvent:)];
        [self kj_swizzleMethod:@selector(touchesMoved:withEvent:) Method:@selector(kj_touchesMoved:withEvent:)];
        [self kj_swizzleMethod:@selector(touchesEnded:withEvent:) Method:@selector(kj_touchesEnded:withEvent:)];
        [self kj_swizzleMethod:@selector(touchesCancelled:withEvent:) Method:@selector(kj_touchesCancelled:withEvent:)];
    });
}
- (void)kj_touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event{
    if (self.kOpenExchange && self.moveblock) {
        CGPoint point = [[touches anyObject] locationInView:self];
        self.moveblock(KJMoveStateTypeBegin,point);
    }
    [self kj_touchesBegan:touches withEvent:event];
}
- (void)kj_touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event{
    if (self.kOpenExchange && self.moveblock) {
        CGPoint point = [[touches anyObject] locationInView:self];
        self.moveblock(KJMoveStateTypeMove,point);
    }
    [self kj_touchesMoved:touches withEvent:event];
}
- (void)kj_touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event{
    if (self.kOpenExchange && self.moveblock) {
        CGPoint point = [[touches anyObject] locationInView:self];
        self.moveblock(KJMoveStateTypeEnd,point);
    }
    [self kj_touchesEnded:touches withEvent:event];
}
- (void)kj_touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event{
    if (self.kOpenExchange && self.moveblock) {
        CGPoint point = [[touches anyObject] locationInView:self];
        self.moveblock(KJMoveStateTypeCancelled,point);
    }
    [self kj_touchesEnded:touches withEvent:event];
}
Property & API

这里定义回调的4种状态,开始触摸,移动,触摸结束和异常中断

typedef NS_ENUM(NSInteger, KJMoveStateType) {
    KJMoveStateTypeBegin = 0,
    KJMoveStateTypeMove,
    KJMoveStateTypeEnd,
    KJMoveStateTypeCancelled,
};
typedef void(^KJMoveBlock)(KJMoveStateType state,CGPoint point);

kOpenExchange 控制是否开启方法交换

- (bool)kOpenExchange{
    return [objc_getAssociatedObject(self,_cmd) boolValue];
}
- (void)setKOpenExchange:(bool)kOpenExchange{
    objc_setAssociatedObject(self,@selector(kOpenExchange),@(kOpenExchange),OBJC_ASSOCIATION_ASSIGN);
}

moveblock 该属性是Touch里面移动回调,需要开启kOpenExchange方法交换才能处理

- (KJMoveBlock)moveblock{
    return (KJMoveBlock)objc_getAssociatedObject(self, _cmd);
}
- (void)setMoveblock:(KJMoveBlock)moveblock{
    objc_setAssociatedObject(self, @selector(moveblock), moveblock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

使用示例

_collectView.kOpenExchange = true;
_weakself;
__block bool move = false;
__block CGPoint beginPoint = CGPointZero;
_collectView.moveblock = ^(KJMoveStateType state, CGPoint point) {
    if (point.y <= 30 && move == false) {
        move = true;
        point = [weakself.collectView convertPoint:point toView:kKeyWindow];
        NSIndexPath *idx = [weakself.collectView indexPathForItemAtPoint:point];
        UICollectionViewCell *nextCell = [weakself.collectView cellForItemAtIndexPath:idx];
        weakself.imageView.backgroundColor = nextCell.backgroundColor;
        weakself.imageView.center = point;
        weakself.imageView.hidden = NO;
    }else if (move && point.y < 30 && state == KJMoveStateTypeMove) {
        point = [weakself.collectView convertPoint:point toView:kKeyWindow];
        weakself.imageView.center = point;
        weakself.collectView.scrollEnabled = NO;
        return;
    }else if (state == KJMoveStateTypeEnd || state == KJMoveStateTypeCancelled) {
        move = false;
        point = [weakself.collectView convertPoint:point toView:kKeyWindow];
        weakself.imageView.center = CGPointZero;
        weakself.imageView.hidden = YES;
    }
    weakself.collectView.scrollEnabled = YES;
};
备注说明

move 开关用来控制滑动距离,满足point.y <= 30 && move == false两个条件的时刻就开启Item的上下移动处理
convertPoint:toView: 将触摸CollectView的点转换到窗口
indexPathForItemAtPoint:cellForItemAtIndexPath:获取到当初触摸的Item,方便取数据

备注:本文用到的部分函数方法和Demo,均来自三方库KJCategories,如有需要的朋友可自行pod 'KJCategories'引入即可

CollectionView左右滚动和上下滑动处理介绍就到此完毕,后面有相关再补充,写文章不容易,还请点个小星星传送门

相关文章

网友评论

      本文标题:iOS UICollectionView左右滚动和上下滑动处理

      本文链接:https://www.haomeiwen.com/subject/lshjyktx.html