移动按钮

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

移动按钮_第1张图片
按钮移动.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;
      }];
    

    }
    }

你可能感兴趣的:(移动按钮)