iOS每日一记之———————————————模仿今日头条栏目选择效果 并且附加cell颤抖效果

嗯 基本上要做的功能就是克雷可深View 的长按 拖动 交换数据 颤抖功能      至于克雷可深View的布局问题就不用我说了吧 都是老生常谈了  嗯 闲话少说 下面上重点代码

//给你的克雷可深View添加一个长安手势和pan手势

self.gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pangestureOperation:)];
        self.longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(onLongPressed:)];
        self.longGesture.minimumPressDuration = 1;
        [self.amphotericCollection addGestureRecognizer:self.longGesture];

//长按的点击方法

  CGPoint point = [sender locationInView:sender.view];
    NSIndexPath *indexPath = [self.amphotericCollection indexPathForItemAtPoint:point];
    // 只允许第一区可移动
    if (indexPath.section != 0) {
        return;
    }
    if (indexPath.section == 0 && indexPath.item == 0) {
        return;
    }
    self.isEditing = YES;
    _isBegin= YES;//开始抖动
    [self.amphotericCollection reloadData];

//添加pan手势 用来拖动

    [self.amphotericCollection addGestureRecognizer:self.gesture];


/拖拽cellItem
- (void)pangestureOperation:(UIPanGestureRecognizer *)sender{
    if (!self.isEditing) {
        return;
    }
    CGPoint point = [sender locationInView:sender.view];
    NSIndexPath *indexPath = [self.amphotericCollection indexPathForItemAtPoint:point];
    // 只允许第一区可移动
    if (indexPath.section != 0) {
        return;
    }
    switch (sender.state) {
            case UIGestureRecognizerStateBegan: {
                if (indexPath) {
                    [self.amphotericCollection beginInteractiveMovementForItemAtIndexPath:indexPath];
                }
                break;
            }
            case UIGestureRecognizerStateChanged: {
                
                NSIndexPath* indexPath = [self.amphotericCollection indexPathForItemAtPoint:[sender locationInView:self.amphotericCollection]];
                if(indexPath.item <1){
                    break;//第一个不可移动  个人限制
                    
                }
                [self.amphotericCollection updateInteractiveMovementTargetPosition:point];
               
               break;
            }
            case UIGestureRecognizerStateEnded: {
                
                [self.amphotericCollection endInteractiveMovement];
                break;
            }
            default: {
                [self.amphotericCollection cancelInteractiveMovement];
                break;
            }
        }
}

//iOS9之后 提供了系统的move方法


//数据源交换方法

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    if (sourceIndexPath.section == 0 && destinationIndexPath.section == 0) {
        [listTop exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
    }
    
}

//之后是克雷可深View的点击方法

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.item == 0) {
            return;
        }
        if (self.isEditing) {
            InlineModel *model = [listTop objectAtIndex:indexPath.item];

//listTop 是上面区的那个数组 listBottom是下面的那个区的数组

            [listTop removeObject:model];
            [listBottom addObject:model];
           
            NSInteger index = listBottom.count - 1;
            if (listBottom.count == 0) {
                index = 0;
            }
            XYSAmphotericCollectionCell *cell = (XYSAmphotericCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
            cell.deleteBtn.hidden = YES;
           [self.amphotericCollection moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:index inSection:1]];
        } else {
            // 选择某一个
            return;
        }
    } else {
        InlineModel *model = [listBottom objectAtIndex:indexPath.item];
        [listBottom removeObject:model];
        [listTop addObject:model];
        
        NSInteger index = listTop.count - 1;
        if (listTop.count == 0) {
            index = 0;
        }
        if (self.isEditing) {
            XYSAmphotericCollectionCell *cell = (XYSAmphotericCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
            cell.deleteBtn.hidden = NO;
        }
       [collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
       
    }

//这里面并没有reloadData方法 是因为 moveItemAtIndexPath这个 用了这个之后 克雷可深View 不会刷新数据 去掉这个之后就可以刷新了 这也是我所困扰的一个问题

所以这里采用刷新区的方法 但是刷新区会有白色的闪光 下面的方法是用来去除闪光效果的

    [self performSelector:@selector(updateData) withObject:self afterDelay:0.3];
}

//用来去除闪光效果的动画
- (void)updateData{
    [UIView animateWithDuration:0 animations:^{
        [self.amphotericCollection performBatchUpdates:^{
            [self.amphotericCollection reloadSections:[NSIndexSet indexSetWithIndex:0]];
            [self.amphotericCollection reloadSections:[NSIndexSet indexSetWithIndex:1]];
            
        } completion: nil];
        
    }];
}



//添加抖动效果
- (void)starLongPress:(XYSAmphotericCollectionCell *)cell{
    CABasicAnimation *animation = (CABasicAnimation *)[cell.layer animationForKey:@"rotation"];
    if (animation == nil) {
        [self shakeImage:cell];
    }else {
        [self resume:cell];
    }
}

- (void)shakeImage:(XYSAmphotericCollectionCell*)cell {
    //创建动画对象,绕Z轴旋转
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //设置属性,周期时长
    [animation setDuration:0.08];
    //抖动角度
    animation.fromValue = @(-M_1_PI/2);
    animation.toValue = @(M_1_PI/2);
    //重复次数,无限大
    animation.repeatCount = HUGE_VAL;
    //恢复原样
    animation.autoreverses = YES;
    //锚点设置为图片中心,绕中心抖动
    cell.layer.anchorPoint = CGPointMake(0.5, 0.5);
    [cell.layer addAnimation:animation forKey:@"rotation"];
    
}
- (void)pause:(XYSAmphotericCollectionCell *)cell {
    
    cell.layer.speed = 0.0;
    
}
- (void)resume:(XYSAmphotericCollectionCell *)cell {
    
    cell.layer.speed = 1.0;
    
}

嗯这样就实现了  是不是很简单呢   至于克雷可深View为什么不能刷新的问题 我也会继续探讨下去的    。。。。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值