您的位置:首页 > 移动开发 > IOS开发

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

2017-06-16 21:19 351 查看
嗯 基本上要做的功能就是克雷可深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为什么不能刷新的问题 我也会继续探讨下去的    。。。。。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 界面 今日头条