相册手势滑动多选效果

本以为很简单的一个功能,深入学习过后才发现细节满满,首先看看iPhone手机的手势多选的gif,根据gif整理出给个细节后再去仿出这样的效果。

iphone相册手势多选效果展示图.gif
一:梳理逻辑(如图)
手势滑动多选逻辑
  • 1.1,获取起始点:获取起始点可以通过手势的状态获取:panGesture.state == UIGestureRecognizerStateBegan,这个状态下获取到CGPoint即是起始点的坐标。
    if (panGesture.state == UIGestureRecognizerStateBegan) {
        // 获取起始点
        self.panSelectStartPoint = [panGesture locationInView:self.collectionView];
        NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:self.panSelectStartPoint];
}
  • 1.2,获取结束点:同样我们也是根据手势的state来获取对应的结束点,这里有个细节需要注意,即需要判断当前手指是否在self.collectionViewitem上,如果不在就不允许手势滑动,这样做的目的是避免和self.collectionView的手势冲突,也是更好的用户体验,更重要的是我们添加的手势是添加到self.view上的,而不是self.collectionView,原因是会和self.collectionViewUIScrollView产生手势冲突,导致不能上下滑动。如下代码:
if (panGesture.state == UIGestureRecognizerStateChanged) {
    NSIndexPath *firstIndexPath = [self.collectionView indexPathForItemAtPoint:self.panSelectStartPoint];
        if (!firstIndexPath) {// 起始点不在cell上直接不可滑动选择
            return;
        }
    NSIndexPath *lastIndexPath = [self.collectionView indexPathForItemAtPoint:currentPoint];
        if (!lastIndexPath) {
            return;
        }
}
  • 1.3,手势的方向:手势的方向可以根据结束点对应cell的minXmaxXminYmaxY来获取对应最新选中的item。

        CGPoint currentPoint = [panGesture locationInView:self.collectionView];
        NSInteger firstLine = 0;
        NSInteger lastLine = 0;
        NSIndexPath *firstIndexPath = [self.collectionView indexPathForItemAtPoint:self.panSelectStartPoint];
        if (!firstIndexPath) {
            // 起始点不在cell上直接不可滑动选择
            return;
        }
        NSIndexPath *lastIndexPath = [self.collectionView indexPathForItemAtPoint:currentPoint];
        if (!lastIndexPath) {
            return;
        }
        NSInteger rowCount = 3;
        if ((firstIndexPath.item + 1) % rowCount == 0) {
            firstLine = (firstIndexPath.item + 1) / rowCount;
        }else {
            firstLine = (firstIndexPath.item + 1) / rowCount + 1;
        }
        if ((lastIndexPath.item + 1) % rowCount == 0) {
            lastLine = (lastIndexPath.item + 1) / rowCount;
        }else {
            lastLine = (lastIndexPath.item + 1) / rowCount + 1;
        }
        NSMutableArray *indexPaths = [NSMutableArray array];
        CGFloat startX;
        CGFloat maxX;
        BOOL xReverse = NO;
        if (currentPoint.x > self.panSelectStartPoint.x) {
            // 向右
            maxX = [self panSelectGetMaxXWithPoint:currentPoint];
            startX = [self panSelectGetMinXWithPoint:self.panSelectStartPoint];
        }else {
            // 向左
            xReverse = YES;
            maxX = [self panSelectGetMaxXWithPoint:self.panSelectStartPoint];
            startX = [self panSelectGetMinXWithPoint:currentPoint];
        }
        CGFloat maxY;
        CGFloat startY;
        BOOL yReverse = NO;
        if (currentPoint.y > self.panSelectStartPoint.y) {
            // 向下
            maxY = [self panSelectGetMaxYWithPoint:currentPoint];
            startY = [self panSelectGetMinYWithPoint:self.panSelectStartPoint];
        }else {
            // 向上
            yReverse = YES;
            maxY = [self panSelectGetMaxYWithPoint:self.panSelectStartPoint];
            startY = [self panSelectGetMinYWithPoint:currentPoint];
        }
        NSInteger distanceW = self.layout.minimumInteritemSpacing + self.layout.itemSize.width;
        NSInteger distanceH = self.layout.minimumInteritemSpacing + self.layout.itemSize.height;

        NSIndexPath *sIndexPath = [self.collectionView indexPathForItemAtPoint:self.panSelectStartPoint];
        if (sIndexPath && ![indexPaths containsObject:sIndexPath]) {
            [indexPaths addObject:sIndexPath];
        }
        while (yReverse ? maxY > startY : startY < maxY) {
            CGFloat tempStartX = startX;
            CGFloat tempMaxX = maxX;
            NSInteger currentLine = 0;
            NSIndexPath *currentIndexPath;
            if (yReverse) {
                currentIndexPath = [self.collectionView indexPathForItemAtPoint:CGPointMake(tempMaxX - 1, maxY - 1)];
            }else {
                currentIndexPath = [self.collectionView indexPathForItemAtPoint:CGPointMake(tempStartX + 1, startY + 1)];
            }
            if ((currentIndexPath.item + 1) % rowCount == 0) {
                currentLine = (currentIndexPath.item + 1) / rowCount;
            }else {
                currentLine = (currentIndexPath.item + 1) / rowCount + 1;
            }
            if (currentLine == firstLine) {
                if (lastLine != firstLine) {
                    if (yReverse) {
                        tempMaxX = [self panSelectGetMaxXWithPoint:self.panSelectStartPoint];
                        tempStartX = 2;
                    }else {
                        if (xReverse) {
                            tempStartX = [self panSelectGetMinXWithPoint:self.panSelectStartPoint];
                        }
                        tempMaxX = SCREEN_WIDTH - 2;
                    }
                }
            }else if (currentLine == lastLine) {
                if (yReverse) {
                    tempStartX = [self panSelectGetMinXWithPoint:currentPoint];
                    tempMaxX = SCREEN_WIDTH - 2;
                }else {
                    tempStartX = 2;
                    if (xReverse) {
                        tempMaxX = [self panSelectGetMaxXWithPoint:currentPoint];
                    }
                }
            }else if (currentLine != firstLine && currentLine != lastLine) {
                tempStartX = 2;
                tempMaxX = SCREEN_WIDTH - 2;
            }
            while (yReverse ? tempMaxX > tempStartX : tempStartX < tempMaxX) {
                NSIndexPath *indexPath;
                if (yReverse) {
                    indexPath = [self panSelectCurrentIndexPathWithPoint:CGPointMake(tempMaxX, maxY) indexPaths:indexPaths];
                }else {
                    indexPath = [self panSelectCurrentIndexPathWithPoint:CGPointMake(tempStartX, startY) indexPaths:indexPaths];
                }
                if (indexPath) {
                    [indexPaths addObject:indexPath];
                }
                if (yReverse) {
                    tempMaxX -= distanceW / 2;
                }else {
                    tempStartX += distanceW / 2;
                }
            }
            if (yReverse) {
                maxY -= distanceH / 2;
            }else {
                startY += distanceH / 2;
            }
        }
        NSIndexPath *eIndexPath = [self.collectionView indexPathForItemAtPoint:currentPoint];
        if (eIndexPath && ![indexPaths containsObject:eIndexPath]) {
            QYPhotoModel *model = self.sources[eIndexPath.item];
            if (self.currentPanSelectType == 0) {
                if (model.selected) {
                    [indexPaths addObject:eIndexPath];
                }
            }else if (self.currentPanSelectType == 1) {
                if (!model.selected) {
                    [indexPaths addObject:eIndexPath];
                }
            }
        }
        if (self.currentPanSelectType == -1) {
            NSIndexPath *firstIndexPath = indexPaths.firstObject;
            QYPhotoModel *firstModel;
            if (firstIndexPath) {
                firstModel = self.sources[firstIndexPath.item];
                self.currentPanSelectType = !firstModel.selected;
            }
        }
        NSMutableArray *reloadSelectArray = [NSMutableArray array];
        for (NSIndexPath *indexPath in indexPaths) {
            QYPhotoModel *model = self.sources[indexPath.item];
            if (self.currentPanSelectType == 0) {
                // 取消选择
                if (model.selected) {
                    [self.manager beforeSelectedListdeletePhotoModel:model];
                    if (![reloadSelectArray containsObject:indexPath]) {
                        [reloadSelectArray addObject:indexPath];
                    }
                }
            }else if (self.currentPanSelectType == 1) {
                // 选择
                if (!model.selected) {
                        [self.manager beforeSelectedListAddPhotoModel:model];
                        if (![reloadSelectArray containsObject:indexPath]) {
                            [reloadSelectArray addObject:indexPath];
                        }
                }else {
                    QYCollectionViewCell *cell = (QYCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
                    if (![cell.selectBtn.currentTitle isEqualToString:model.selectIndexStr] || !cell.selectBtn.selected) {
                        if (![reloadSelectArray containsObject:indexPath]) {
                            [reloadSelectArray addObject:indexPath];
                        }
                    }
                }
            }
        }
        NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",indexPaths];
        NSArray * filterArray = [self.panSelectIndexPaths filteredArrayUsingPredicate:filterPredicate];
        for (NSIndexPath *indexPath in filterArray) {
            QYPhotoModel *model = self.sources[indexPath.item];
            if (self.currentPanSelectType == 0) {
                if (!model.selected) {
                    [self.manager beforeSelectedListAddPhotoModel:model];
                    if (![reloadSelectArray containsObject:indexPath]) {
                        [reloadSelectArray addObject:indexPath];
                    }
                }
            }else if (self.currentPanSelectType == 1) {
                if (model.selected) {
                    [self.manager beforeSelectedListdeletePhotoModel:model];
                    if (![reloadSelectArray containsObject:indexPath]) {
                        [reloadSelectArray addObject:indexPath];
                    }
                }
            }
        }
        if (self.currentPanSelectType == 0) {
            for (QYPhotoModel *model in [self.manager selectedArray]) {
                if (!model.dateCellIsVisible) {
                    continue;
                }
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[self dateItem:model] inSection:0];
                QYCollectionViewCell *cell = (QYCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
                if (cell && ![cell.selectBtn.currentTitle isEqualToString:model.selectIndexStr]) {
                    if (![reloadSelectArray containsObject:indexPath]) {
                        [reloadSelectArray addObject:indexPath];
                    }
                }
            }
        }
        [reloadSelectArray addObjectsFromArray:[self getVisibleVideoCellIndexPathsWithCurrentIndexPaths:reloadSelectArray]];
        if (reloadSelectArray.count) {
            [self.collectionView reloadItemsAtIndexPaths:reloadSelectArray];
        }
        self.panSelectIndexPaths = indexPaths;
        for (NSIndexPath *item in reloadSelectArray) {
            NSLog(@"当前的刷新的item: %ld", item.item);
        }
    }else if (panGesture.state == UIGestureRecognizerStateEnded ||
              panGesture.state == UIGestureRecognizerStateCancelled) {
        self.panSelectIndexPaths = nil;
        self.currentPanSelectType = -1;
    }

#pragma mark - private
- (CGFloat)panSelectGetMaxXWithPoint:(CGPoint)point {
    
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    return CGRectGetMaxX(cell.frame) - 2;
}

- (CGFloat)panSelectGetMinXWithPoint:(CGPoint)point {
    
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    return cell.qy_x + 2;
}

- (CGFloat)panSelectGetMaxYWithPoint:(CGPoint)point {
    
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    return CGRectGetMaxY(cell.frame) - 2;
}

- (CGFloat)panSelectGetMinYWithPoint:(CGPoint)point {
    
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    return cell.qy_y + 2;
}

注意点:如上gif,手势在滑动过程中有选中和取消选中,可以在model中自定一个一个isSelected的BOOL值来记录item是否已被选中。我的思路是新增一个manager管理类,用于记录当前手势是选中还是取消选中,最后给选中的item做个标记,便于设置item选中时设置一个白色蒙版(设置的标记和item的row是一样就选中或者取消选中)具体详情见demo即可。

  • 1.4, manager管理类进行选中的标记
- (void)beforeSelectedDeleteItemPhotoModel:(QYPhotoModel *)model {
    
    if (![self.selectedList containsObject:model]) {
        return;
    }
    model.selected = NO;
    model.selectIndexStr = @"";
    model.selectedIndex = 0;

    [self.selectedList removeObject:model];
    
    int i = 0;
    for (QYPhotoModel *model in self.selectedList) {
        model.selectIndexStr = [NSString stringWithFormat:@"%d",i + 1];
        i++;
    }
}

- (void)beforeSelectedAddItemPhotoModel:(QYPhotoModel *)model {

    [self.selectedList addObject:model];
    model.selected = YES;
    model.selectedIndex = [self.selectedList indexOfObject:model];
    model.selectIndexStr = [NSString stringWithFormat:@"%ld",model.selectedIndex  + 1];
//    NSLog(@"添加的图片: %@", model.selectIndexStr );
}

- (NSArray *)selectedArray {
    return self.selectedList;
}

二:最后效果图如下:

最终效果展示图

总结

功能整体思路大体就是这样,具体在计算手势方向,item标记等逻辑如上述代码展示,可细细理解。由于公司项目需要,需要深入学习和使用OpenGL ES的知识。接下来的目标是学习OpenGL ES、和音视频相关知识,也会不断将笔记更新,继续督促自己进步。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容