关于 控件抖动以及 互换位置的说写

2024-06-18 22:08
文章标签 位置 控件 抖动 互换

本文主要是介绍关于 控件抖动以及 互换位置的说写,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

    前段时间由于公司需求,恰好需要做一个CollectionView的Item长按后抖动并且可移动效果。但由于一些原因,当时并没有来得及去处理,所以一直心有遗憾。目前市场上此功能并不少见,而且在github上也有一些类似的开源代码,所以其实总结来说:首先并不能作为一个功能难点,只能说是兴趣至此;其次也是真心希望能帮助一些我能帮助的人,以及希望大家能给些建议。都说不想当将军的士兵不是好士兵,所以我觉得,不能溜溜的马始终都是骡子...嘿嘿...回归正题...(可以复制此链接浏览器下载demo  http://git.oschina.net/JHissuperman/CollectionView)

1.首先是UICollectionView的创建:

    //创建一个layout布局类

    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];

    //设置布局方向为垂直流布局

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    //设置每个item的大小为127.5*127.5

    layout.itemSize = CGSizeMake(114*kWidth/750.00, 114*kWidth/750.00);

    //整体view据上左下右距离

    layout.sectionInset = UIEdgeInsetsMake(48*kWidth/750.00, 48*kWidth/750.00, 48*kWidth/750.00,48*kWidth/750.00);

    //每个item上下距离

    layout.minimumLineSpacing = 90*kWidth/750.00;

    //每个item左右距离

    layout.minimumInteritemSpacing = 66*kWidth/750.00;

    //创建collectionView 通过一个布局策略layout来创建

    _vibrate = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0*kWidth/750.00, kWidth, kHeight) collectionViewLayout:layout];

    _vibrate.delegate = self;

    _vibrate.dataSource = self;

    _vibrate.backgroundColor = [UIColor lightGrayColor];

    _vibrate.showsHorizontalScrollIndicator = NO;

    _vibrate.showsVerticalScrollIndicator = NO;

    _vibrate.userInteractionEnabled = YES;

    //注册item类型 这里使用系统的类型

    [_vibrate registerClass:[VibrateCollectionViewCell class] forCellWithReuseIdentifier:@"vibrate"];

    [self.view addSubview:_vibrate];

2.然后实现collectionview的代理方法:

//返回分区个数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;

}

 

//返回每个分区的item个数

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return _collectionArr.count;

}

 

//返回每个item

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    VibrateCollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"vibrate" forIndexPath:indexPath];

//    [cell sizeToFit];

    if(!cell){

        NSLog(@"-----------");

    }

    NSInteger num = indexPath.row;

 

    cell.nameLable.text = _collectionArr[num];

    cell.headImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"00%@",_collectionArr[num]]];

    if(_isBegin == YES ){

        [self starLongPress:cell];

    }

 

    return cell;

}

3.添加手势

3.1  抖动手势的添加

- (void)addRecognize{

    //添加长按抖动手势

    if(!_recognize){

        _recognize = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

    }

    //长按响应时间

    _recognize.minimumPressDuration = 1;

    [_vibrate addGestureRecognizer:_recognize];

}

- (void)longPress:(UILongPressGestureRecognizer *)longGesture {

    //判断手势状态

    switch (longGesture.state) {

        case UIGestureRecognizerStateBegan:{

            //判断手势落点位置是否在路径上

            NSIndexPath *indexPath = [self.vibrate indexPathForItemAtPoint:[longGesture locationInView:self.vibrate]];

            if (indexPath.row >= 0) {//第一个不可移动  个人限制

                _isBegin = YES;

                [_vibrate removeGestureRecognizer:_recognize];

                [self addLongGesture];

                [self addSureButton];

                [_vibrate reloadData];

                NSLog(@"1");

            }else{

                break;

            }

        }

            break;

        case UIGestureRecognizerStateChanged:{

            NSLog(@"2");

            break;

        }

        case UIGestureRecognizerStateEnded:

            NSLog(@"3");

            break;

        default:

            NSLog(@"4");

            break;

    }

}

 

//开始抖动

- (void)starLongPress:(VibrateCollectionViewCell*)cell{

    CABasicAnimation *animation = (CABasicAnimation *)[cell.layer animationForKey:@"rotation"];

    if (animation == nil) {

        [self shakeImage:cell];

    }else {

        [self resume:cell];

    }

}

 

//这个参数的理解比较复杂,我的理解是所在layer的时间与父layer的时间的相对速度,为1时两者速度一样,为2那么父layer过了一秒,而所在layer过了两秒(进行两秒动画),为0则静止。

- (void)pause:(VibrateCollectionViewCell*)cell {

    cell.layer.speed = 0.0;

}

 

- (void)resume:(VibrateCollectionViewCell*)cell {

    cell.layer.speed = 1.0;

}

 

 

- (void)shakeImage:(VibrateCollectionViewCell*)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"];

}

3.2 移动手势的添加

- (void)addLongGesture{

    //此处给其增加长按手势,用此手势触发cell移动效果

    if(!_longGesture){

        _longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];

    }

    _longGesture.minimumPressDuration = 0;

    [_vibrate addGestureRecognizer:_longGesture];

}

//监听手势,并设置其允许移动cell和交换资源

 

- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {

    //判断手势状态

    switch (longGesture.state) {

        case UIGestureRecognizerStateBegan:{

            //判断手势落点位置是否在路径上

            NSIndexPath *indexPath = [self.vibrate indexPathForItemAtPoint:[longGesture locationInView:self.vibrate]];

            if (indexPath.row > 0) {//第一个不可移动  个人限制

                [_vibrate beginInteractiveMovementForItemAtIndexPath:indexPath];

            }else{

                break;

            }

        }

            break;

        case UIGestureRecognizerStateChanged:{

            NSIndexPath* indexPath = [_vibrate indexPathForItemAtPoint:[longGesture locationInView:_vibrate]];

            if(indexPath.row<1){

                break;//第一个不可移动  个人限制

            }

            //移动过程当中随时更新cell位置

            [_vibrate updateInteractiveMovementTargetPosition:[longGesture locationInView:_vibrate]];

            break;

        }

        case UIGestureRecognizerStateEnded:

            //移动结束后关闭cell移动

            [_vibrate endInteractiveMovement];

            break;

        default:

            [_vibrate endInteractiveMovement];

 

//            [_vibrate cancelInteractiveMovement];

            break;

    }

}

 

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

 

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {

    

    //取出源item数据

    id objc = [_collectionArr objectAtIndex:sourceIndexPath.item];

    //从资源数组中移除该数据

    [_collectionArr removeObject:objc];

    //将数据插入到资源数组中的目标位置上

    [_collectionArr insertObject:objc atIndex:destinationIndexPath.item];

//    [_vibrate reloadData];

}

具体效果  可以复制此链接浏览器下载demo  http://git.oschina.net/JHissuperman/CollectionView

希望能帮助到一些人,也希望大家能够指正一些问题。请大家多多留言发表意见

这篇关于关于 控件抖动以及 互换位置的说写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073187

相关文章

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

PyCharm如何更改缓存位置

《PyCharm如何更改缓存位置》:本文主要介绍PyCharm如何更改缓存位置的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录PyCharm更改缓存位置1.打开PyCharm的安装编程目录2.将config、sjsystem、plugins和log的路径

WinForms中主要控件的详细使用教程

《WinForms中主要控件的详细使用教程》WinForms(WindowsForms)是Microsoft提供的用于构建Windows桌面应用程序的框架,它提供了丰富的控件集合,可以满足各种UI设计... 目录一、基础控件1. Button (按钮)2. Label (标签)3. TextBox (文本框

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C#实现WinForm控件焦点的获取与失去

《C#实现WinForm控件焦点的获取与失去》在一个数据输入表单中,当用户从一个文本框切换到另一个文本框时,需要准确地判断焦点的转移,以便进行数据验证、提示信息显示等操作,本文将探讨Winform控件... 目录前言获取焦点改变TabIndex属性值调用Focus方法失去焦点总结最后前言在一个数据输入表单

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2