IOS 实现Button长按放大 并且可以移动互换位置


#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>


typedef enum tagBorderType {

    BorderTypeDashed,

    BorderTypeSolid

} BorderType;


@interface LBorderView : UIView

{

    CAShapeLayer *_shapeLayer;

    BorderType _borderType;

    CGFloat _cornerRadius;

    CGFloat _borderWidth;

    NSUInteger _dashPattern;

    NSUInteger _spacePattern;

    UIColor *_borderColor;

}


@property (assign ,nonatomic) BorderType borderType;

@property (assign ,nonatomic) CGFloat cornerRadius;

@property (assign ,nonatomic) CGFloat borderWidth;

@property (assign ,nonatomic) NSUInteger dashPattern;

@property (assign ,nonatomic) NSUInteger spacePattern;

@property (strong ,nonatomic) UIColor *borderColor;


}



#import "LBorderView.h"


@implementation LBorderView


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        [self initalizeAppearance];

    }

    return self;

}



- (void)initalizeAppearance

{

    self.backgroundColor = [UIColor clearColor];

    _borderWidth = 0.3f;

    _dashPattern = 2;

    _spacePattern = 1;

    _borderType = BorderTypeDashed;

}


- (void)drawRect:(CGRect)rect

{

    if (_shapeLayer) [_shapeLayer removeFromSuperlayer];

    CGFloat cornerRadius = _cornerRadius;

    CGFloat borderWidth = _borderWidth;

    CGFloat dashPattern = _dashPattern;

    CGFloat spacePattern = _spacePattern;

    UIColor *borderColor = _borderColor ? _borderColor : [UIColor blackColor];

    CGRect frame = self.bounds;

    

    _shapeLayer = [CAShapeLayer layer];

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius);

    CGPathAddLineToPoint(path, NULL, 0, cornerRadius);

    

    

    CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);

    CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);

    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);

    CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height-cornerRadius);

    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO);

    CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);

    CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);

    

    _shapeLayer.path = path;

    _shapeLayer.backgroundColor = [UIColor clearColor].CGColor;

    _shapeLayer.frame = frame;

    _shapeLayer.masksToBounds = NO;

    [_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];

    _shapeLayer.fillColor = [UIColor clearColor].CGColor;

    _shapeLayer.strokeColor = [borderColor CGColor];

    _shapeLayer.lineWidth = borderWidth;

    _shapeLayer.lineDashPattern = _borderType == BorderTypeDashed ? [NSArray arrayWithObjects:[NSNumber numberWithInt:dashPattern],[NSNumber numberWithInt:spacePattern] ,nil] : nil;

    _shapeLayer.lineCap = kCALineCapRound;

    [self.layer addSublayer:_shapeLayer];

    self.layer.cornerRadius = cornerRadius;

    

}






// =================================================ViewController=================================


//

//  ViewController.m

//  图标拖动

//

//  Created by Mac on 16/1/25.

//  Copyright © 2016 gaoqingbiao. All rights reserved.

//


#import "ViewController.h"

#import "LBorderView.h"

#define UIColorFromRGB(rgbValue) [UIColor \

colorWithRed:((float)((rgbValue & 0xFF0000) >>16))/255.0 \

green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \

blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]


#define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.f green:g/255.f blue:b/255.f alpha:1]


@interface ViewController ()

{

    CGPoint _ButtonPoint;

    CGPoint startPoint;

    

    NSInteger beginPos;

    NSInteger endPos;

    

}

@property (strong ,nonatomic) NSArray *textArray;

@property (strong ,nonatomic) NSMutableArray *deshArray;

@property (strong ,nonatomic) NSMutableArray *buttonArray;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = RGBCOLOR(239, 239, 239);

    self.view.alpha = 1.0f;

    _textArray = @[@"我的关注",@"逛一逛",@"搞笑集锦",@"团战集锦",@"编辑选推"];

    [self initalizeAppearance];


}


- (void)initalizeAppearance

{

    _deshArray = [NSMutableArray array];

    int itemWidth = ((self.view.bounds.size.width - 50) / 4);

    for (int i = 0; i < _textArray.count + 1; i++) {

        CGRect frame = CGRectMake((i % 4) * (itemWidth + 10) +10, 35 + (i / 4) * 40 + 5, itemWidth, 30);

        frame = CGRectInset(frame, 1, 1);

        LBorderView *lb = [[LBorderView alloc] initWithFrame:frame];

        [self.view addSubview:lb];

        [_deshArray addObject:lb];

    }

    [self initMenuButton:_textArray];

}


- (void)initMenuButton:(NSArray *)array

{

    _buttonArray = [NSMutableArray array];

    int buttonWidth = ((self.view.bounds.size.width - 50) / 4);

    for (int i = 0; i < array.count; i ++) {

        UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        menuButton.tag = i;

        menuButton.frame = CGRectMake((i%4)*(buttonWidth+10)+10,35+(i/4)*40+5,buttonWidth,30);

        menuButton.backgroundColor = [UIColor whiteColor];

        [menuButton setTitle:array[i] forState:UIControlStateNormal];

        [menuButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        menuButton.titleLabel.font = [UIFont systemFontOfSize:14];

        menuButton.layer.borderColor = UIColorFromRGB(0xe5e5e5).CGColor;;

        menuButton.layer.borderWidth = 0.3f;

        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerAction:)];

        [menuButton addGestureRecognizer:longGesture];

        [self.view addSubview:menuButton];

        [self.buttonArray addObject:menuButton];

    }

    NSLog(@"_buttonArray = %@",_buttonArray);

}


- (void)longPressGestureRecognizerAction:(UILongPressGestureRecognizer *)sender

{

    UIButton *btn = (UIButton *)sender.view;

    if (sender.state == UIGestureRecognizerStateBegan) {

        

        startPoint = [sender locationInView:sender.view];

        beginPos = btn.tag;

        _ButtonPoint = btn.center;

          NSLog(@"btn = %@",btn);

        NSLog(@"btn.tag = %lu",btn.tag);

        [UIView animateWithDuration:0.2 animations:^{

            btn.transform = CGAffineTransformMakeScale(1.1, 1.1);

            btn.alpha = 0.7;

            

        }];

    } else if (sender.state == UIGestureRecognizerStateChanged){

        CGPoint newPoint = [sender locationInView:sender.view];

        CGFloat deltaX = newPoint.x - startPoint.x;

        CGFloat deltaY = newPoint.y - startPoint.y;

        btn.center = CGPointMake(btn.center.x + deltaX, btn.center.y + deltaY);

        NSInteger fromIndex = btn.tag;

        

        NSInteger toIndex = [self judgeMoveByButtonPoint:btn.center moveButton:btn];


        if (toIndex < 0) {

            return;

        } else {

            btn.tag = toIndex;

            // 向后移动

            if (fromIndex - toIndex < 0) {

                for (NSInteger i = fromIndex; i < toIndex; i ++) {

                    UIButton *nextBtn = _buttonArray[i+1];

                    // 改变按钮中心点的位置

                    CGPoint temp = nextBtn.center;

                    [UIView animateWithDuration:0.5 animations:^{

                        nextBtn.center = _ButtonPoint;

                    }];

                    _ButtonPoint = temp;

                    // 交换tag

                    nextBtn.tag = i;


                }

                [self sortArray];

            } else if (fromIndex - toIndex > 0) {

                // 向前移动

                for (NSInteger i = fromIndex; i > toIndex; i --) {

                    UIButton *beforBtn = _buttonArray[i - 1];

                    CGPoint temp = beforBtn.center;

                    [UIView animateWithDuration:0.5 animations:^{

                        beforBtn.center = _ButtonPoint;

                    }];

                    _ButtonPoint = temp;

                    beforBtn.tag = i;

                }

                [self sortArray];

            }

            


        }

        

    }

    

    

    else {

        [UIView animateWithDuration:0.2 animations:^{

            btn.transform = CGAffineTransformIdentity;

            btn.alpha = 1.0f;

            btn.center = _ButtonPoint;

        }];

    }

}



- (void)sortArray

{

   // 对已改变按钮的数组进行排序

    [_buttonArray sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

        UIButton *temp1 = (UIButton *)obj1;

        UIButton *temp2 = (UIButton *)obj2;

        return temp1.tag > temp2.tag;    //tag值大的按钮向后移

    }];


}


- (NSInteger)judgeMoveByButtonPoint:(CGPoint)point moveButton:(UIButton *)btn

{

    /**

     * 判断移动按钮的中心点是否包含了所在按钮的中心点如果是将i返回

     */

    for (NSInteger i = 0; i < _buttonArray.count; i++) {

        UIButton *button = _buttonArray[i];

        if (!btn || button != btn) {

            if (CGRectContainsPoint(button.frame, point)) {

                return i;

            }

        }

    }

    return -1;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];


}


@end



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值