實現控件拖動的方法有多種,能夠使用UICollectionView的代理方法直接實現,可是有些開發者在初始時沒有使用UICollectionView建立九宮格,後來增長需求,卻要增長這種拖動移動的效果,又不想更改頁面的初始控件,那麼應該怎麼實現呢?數組
方法很簡單,首先在@interface建立如下全局變量;佈局
@interface YRViewController () { BOOL contain; CGPoint startPoint; CGPoint originPoint; } @property (strong , nonatomic) NSMutableArray *itemArray; @end
如圖所示,在viewDidLoad裏建立了以下的控件佈局;atom
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 }
下面,咱們就要思考,如何實現長按移動了,控件的移動而且從新排序,主要是考慮移動的那個控件移動到新的位置,直接看下列代碼,spa
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 }
這樣,咱們經過位置判斷,讓控件的位置得以改變。code