移动按钮

作者: 江水东流 | 来源:发表于2016-10-24 18:42 被阅读124次

    先上效果图
    github地址 https://github.com/1271284056/Moving-Buttons.git

    按钮移动.gif
    1. 设置一个baseTag给每个按钮从100标记,可以通过 viewWithTag 方法获取按钮(tag为0时候是控制器的view)

    ViewController.h

    import "ViewController.h"
    @interface ViewController (){
    NSInteger totalNumer;
    CGPoint nextPoint;//下一个按钮的位置
    CGPoint valuePoint;//记录移动按钮的位置
    }

    ViewController.m

    用九宫格算法创建9个按钮,给每一个按钮添加一个长按手势,移动按钮时候用(CGRectContainsPoint(btn2.frame, touchPoint)判断移动按钮和其它按钮的相对位置,如果移动到某一个按钮上面,用tag判断,实时移动其它按钮达成效果,代码如下:

    • (void)longPress:(UIGestureRecognizer *)recongize{
      UIButton *btn = (UIButton *)recongize.view;
      for (UIButton *btn1 in self.view.subviews) {
      if (btn1 != btn) {
      //禁用没被选中按钮的手势
      btn1.userInteractionEnabled = NO;
      }
      }
      //取得手势的位置
      CGPoint touchPoint = [recongize locationInView:self.view];
      if (recongize.state == UIGestureRecognizerStateBegan) {
      //开始移动
      [UIView animateWithDuration:0.2 animations:^{
      btn.transform = CGAffineTransformMakeScale(1.3, 1.3);
      btn.alpha = 0.7;
      //把选中按钮放到最前面
      [self.view bringSubviewToFront:btn];
      valuePoint = btn.center;
      }]; }else if (recongize.state == UIGestureRecognizerStateChanged){//移动view
      btn.center = touchPoint;
      for (UIButton *btn2 in self.view.subviews) {
      //遍历按钮 如果某一个按钮frame包含移动的按钮
      if (CGRectContainsPoint(btn2.frame, touchPoint)&&btn2 != btn) {
      //开始按钮索引
      NSInteger startIndex = btn.tag - KBase_tag;
      //结束按钮索引
      NSInteger endIndex = btn2.tag - KBase_tag;

                if (endIndex > startIndex) {//往后移
                    [UIView animateWithDuration:0.2 animations:^{
                        for (NSInteger i = startIndex+1; i<=endIndex; i++) {
                            //取出后面的每一个按钮
                            UIButton *button = (UIButton *)[self.view viewWithTag:i+KBase_tag];
                            //记录下一个按钮的中心位置
                            nextPoint = button.center;
                            //移动下一个按钮到上一个位置
                            button.center = valuePoint;
                            //把下一个按钮的位置保存成当前按钮位置
                            valuePoint = nextPoint;
                            //tag减少
                            button.tag--;
                            //修改title
                            [button setTitle:[NSString stringWithFormat:@"第%ld",button.tag] forState:UIControlStateNormal];
                        }
                        btn.tag = endIndex + KBase_tag;
                        [btn setTitle:[NSString stringWithFormat:@"第%ld",btn.tag] forState:UIControlStateNormal];
                    }];
            }else {//往前移
                    [UIView animateWithDuration:0.2 animations:^{
                        for (NSInteger i = startIndex-1; i>=endIndex; i--) {
                            UIButton * button3 = (UIButton *)[self.view viewWithTag:i+KBase_tag];
                            nextPoint = button3.center;
                            button3.center = valuePoint;
                            valuePoint = nextPoint;
                            button3.tag++;
                            [button3 setTitle:[NSString stringWithFormat:@"第%ld",button3.tag] forState:UIControlStateNormal];
                        }
                        btn.tag = endIndex+KBase_tag;
                        [btn setTitle:[NSString stringWithFormat:@"第%ld",btn.tag] forState:UIControlStateNormal];
                    }];
                }
            }
        } }else if (recongize.state == UIGestureRecognizerStateEnded){
        //移动结束
        for (UIButton * bt in self.view.subviews) {
            if (bt!=btn) {
                bt.userInteractionEnabled = YES;
            }
        }
        // 结束时候恢复view的外观(放大,改变颜色等)
        [UIView animateWithDuration:0.2 animations:^{
            btn.transform = CGAffineTransformMakeScale(1.0, 1.0);
            btn.alpha = 1;
            btn.center = valuePoint;
        }];
      

      }
      }

    相关文章

      网友评论

      • iOS阿能:楼主写的很不错,我现在再做一个类型QQ空间一个发送文字跟图片,下一步应该会学习你这个功能,给加到项目里面去
        江水东流:@这货是谁 嗯 图片可以排序
      • 江水东流:来看的写点评论呗,也看看其它文章

      本文标题:移动按钮

      本文链接:https://www.haomeiwen.com/subject/revluttx.html