重写 View 的 Touch 方法,实现一个酷炫的九宫格图片

前几天翻看代码库,发现一个之前写过的一个有意思的小玩意,共享给大家😄
废话不多说,上图,先看看效果
photosView.gif

怎么样,是不是还蛮有意思呢?

实现起来非常简单,我们只需要重写几个 View 的 touch 方法

//触摸开始
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //获取触摸点
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    //当前点击到的图片的下标小于图片数组的元素个数
    _selectIndex = [self itemIndexWithPoint:point];
    if (_selectIndex < self.itemArray.count) {
        UIImageView *item = self.itemArray[_selectIndex];
        //拿到最上层
        [self bringSubviewToFront:item];
        //动画效果
        [UIView animateWithDuration:0.3 animations:^{
            //改变当前选中图片视图的大小和位置
            item.center = point;
            item.transform = CGAffineTransformMakeScale(1.2, 1.2);
            item.alpha = 0.8;
        }];
    }
    
}

在触摸一开始,我们先判定当前触摸的点是在哪一张图片上,获得这张图片的下标,并设置为选中下标,然后改变当前图片的位置(中心移动到触摸点)和大小(放大效果)。

//触摸移动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //获取触摸点
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    //获取当前触摸点位置下标
    NSInteger index = [self itemIndexWithPoint:point];
    
    if (_selectIndex < self.itemArray.count) {
        UIImageView *item = self.itemArray[_selectIndex];
        item.center = point;
        if (index < self.itemArray.count && index != _selectIndex) {
            //当前点位置所属下标与选中下标不同
            //将两个图片分别在数据源数组和子视图数组中移除
            UIImage *image = _dataList[_selectIndex];
            [_dataList removeObjectAtIndex:_selectIndex];
            [self.itemArray removeObjectAtIndex:_selectIndex];
            //重新插入到指定位置
            [_dataList insertObject:image atIndex:index];
            [self.itemArray insertObject:item atIndex:index];
            //重新记录选中下标
            _selectIndex = index;
            //重新布局
            [self restartMakeItemFram];
        }
    }
    
}

然后在触摸移动方法中再次判定当前触摸点所在的图片下标,然后比较选中下标与当前下标,如果不相同,就交换两张图片的位置。

//触摸结束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    if (_selectIndex < _itemArray.count) {
        UIImageView *item = _itemArray[_selectIndex];
        
        //还原操作
        [UIView animateWithDuration:0.3 animations:^{
            item.transform = CGAffineTransformIdentity;
            item.alpha = 1;
            item.frame = [self makeFrameWithIndex:(int)_selectIndex];
        }];
    }
    
}

最后,在触摸结束方法中还原选中图片的大小,重新计算它的位置

是不是很简单呢?下面附上 Demo 的地址
Demo点这里点这里

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,718评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • ( 郭宁Judy头马CC3演讲稿) Do you think Harvard is the university ...
    大J小M成长记阅读 682评论 0 2
  • 急救室外,小平母亲泣不成声…… 12月3日下午15点零3分,星期三。天蓝得一丝不挂,微冷,风儿也歇息了。再寻常不过...
    十九M阅读 395评论 2 3
  • I didn't like the implication. And even though my dad had...
    柒弟阅读 1,134评论 0 2