实现控件拖动的方法有多种,可以使用UICollectionView的代理方法直接实现,但是有些开发者在初始时没有使用UICollectionView创建九宫格,后来增加需求,却要增加这种拖动移动的效果,又不想更改页面的初始控件,那么应该怎么实现呢?
方法很简单,首先在@interface创建以下全局变量;
@interface YRViewController () { BOOL contain; CGPoint startPoint; CGPoint originPoint; } @PRoperty (strong , nonatomic) NSMutableArray *itemArray; @end
如图所示,在viewDidLoad里创建了如下的控件布局;
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 for (NSInteger i = 0;i<8;i++) 6 { 7 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 8 btn.backgroundColor = [UIColor redColor]; 9 btn.frame = CGRectMake(50+(i%2)*100, 64+(i/2)*100, 90, 90); 10 btn.tag = i; 11 btn.titleLabel.font = [UIFont boldSystemFontOfSize:20]; 12 [btn setTitle:[NSString stringWithFormat:@"%d",1+i] forState:UIControlStateNormal]; 13 [self.view addSubview:btn]; 14 UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressed:)]; 15 [btn addGestureRecognizer:longGesture]; 16 [self.itemArray addObject:btn]; 17 18 } 19 }
下面,我们就要思考,如何实现长按移动了,控件的移动并且重新排序,主要是考虑移动的那个控件移动到新的位置,直接看下列代码,
1 - (void)buttonLongPressed:(UILongPressGestureRecognizer *)sender 2 { 3 4 UIButton *btn = (UIButton *)sender.view; 5 if (sender.state == UIGestureRecognizerStateBegan) 6 { 7 startPoint = [sender locationInView:sender.view]; 8 originPoint = btn.center; 9 [UIView animateWithDuration:Duration animations:^{ 10 btn.transform = CGAffineTransformMakeScale(1.1, 1.1); 11 btn.alpha = 0.7; 12 }]; 13 14 } 15 else if (sender.state == UIGestureRecognizerStateChanged) 16 { 17 18 CGPoint newPoint = [sender locationInView:sender.view]; 19 CGFloat deltaX = newPoint.x-startPoint.x; 20 CGFloat deltaY = newPoint.y-startPoint.y; 21 btn.center = CGPointMake(btn.center.x+deltaX,btn.center.y+deltaY); 22 //NSLog(@"center = %@",NSStringFromCGPoint(btn.center)); 23 NSInteger index = [self indexOfPoint:btn.center withButton:btn]; 24 if (index<0) 25 { 26 contain = NO; 27 } 28 else 29 { 30 [UIView animateWithDuration:Duration animations:^{ 31 32 CGPoint temp = CGPointZero; 33 UIButton *button = _itemArray[index]; 34 temp = button.center; 35 button.center = originPoint; 36 btn.center = temp; 37 originPoint = btn.center; 38 contain = YES; 39 40 }]; 41 } 44 } 45 else if (sender.state == UIGestureRecognizerStateEnded) 46 { 47 [UIView animateWithDuration:Duration animations:^{ 48 49 btn.transform = CGAffineTransformIdentity; 50 btn.alpha = 1.0; 51 if (!contain) 52 { 53 btn.center = originPoint; 54 } 55 }]; 56 } 57 }
下面这个方法用来判断要移动的button将要移动到的位置的center是不是在button数组(itemArray)元素的位置中,如果是,返回i 新位置原来的button是数组中得第几个元素,否则,返回-1。
1 - (NSInteger)indexOfPoint:(CGPoint)point withButton:(UIButton *)btn 2 { 3 for (NSInteger i = 0;i<_itemArray.count;i++) 4 { 5 UIButton *button = _itemArray[i]; 6 if (button != btn) 7 { 8 if (CGRectContainsPoint(button.frame, point)) 9 { 10 return i; 11 } 12 } 13 } 14 return -1; 15 }
这样,我们通过位置判断,让控件的位置得以改变。