你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> iOS如何實現長按拖動控件

iOS如何實現長按拖動控件

編輯:IOS技巧綜合
[摘要]本文是對iOS如何實現長按拖動控件的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。

  實現控件拖動的方法有多種,可以使用UICollectionView的代理方法直接實現,但是有些開發者在初始時沒有使用UICollectionView創建九宮格,後來增加需求,卻要增加這種拖動移動的效果,又不想更改頁面的初始控件,那麼應該怎麼實現呢?

  方法很簡單,首先在@interface創建以下全局變量;

@interface YRViewController ()
{
    BOOL contain;
     CGPoint startPoint;
     CGPoint originPoint;
   
}
@property (strong , nonatomic) NSMutableArray *itemArray;
@end

  如圖所示,在viewDidLoad裡創建了如下的控件布局;

 - (void)viewDidLoad
 {
     [super viewDidLoad];
     
     for (NSInteger i = 0;i<8;i++)
     {
         UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
         btn.backgroundColor = [UIColor redColor];
         btn.frame = CGRectMake(50+(i%2)*100, 64+(i/2)*100, 90, 90);
         btn.tag = i;
         btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
         [btn setTitle:[NSString stringWithFormat:@"%d",1+i] forState:UIControlStateNormal];
         [self.view addSubview:btn];
         UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressed:)];
         [btn addGestureRecognizer:longGesture];
         [self.itemArray addObject:btn];
 
     }
 }

  下面,我們就要思考,如何實現長按移動了,控件的移動並且重新排序,主要是考慮移動的那個控件移動到新的位置,直接看下列代碼,

 - (void)buttonLongPressed:(UILongPressGestureRecognizer *)sender
 {
 
     UIButton *btn = (UIButton *)sender.view;
     if (sender.state == UIGestureRecognizerStateBegan)
     {
         startPoint = [sender locationInView:sender.view];
         originPoint = btn.center;
         [UIView animateWithDuration:Duration 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);
         //NSLog(@"center = %@",NSStringFromCGPoint(btn.center));
         NSInteger index = [self indexOfPoint:btn.center withButton:btn];
         if (index<0)
         {
             contain = NO;
         }
         else
         {
             [UIView animateWithDuration:Duration animations:^{
                 
                 CGPoint temp = CGPointZero;
                 UIButton *button = _itemArray[index];
                 temp = button.center;
                 button.center = originPoint;
                 btn.center = temp;
                 originPoint = btn.center;
                 contain = YES;
 
             }];
         } 
     }
     else if (sender.state == UIGestureRecognizerStateEnded)
     {
         [UIView animateWithDuration:Duration animations:^{
             
            btn.transform = CGAffineTransformIdentity;
            btn.alpha = 1.0;
             if (!contain)
             {
                  btn.center = originPoint;
             }
         }];
     }
 }

  下面這個方法用來判斷要移動的button將要移動到的位置的center是不是在button數組(itemArray)元素的位置中,如果是,返回i 新位置原來的button是數組中得第幾個元素,否則,返回-1。

 - (NSInteger)indexOfPoint:(CGPoint)point withButton:(UIButton *)btn
 {
     for (NSInteger i = 0;i<_itemArray.count;i++)
     {
         UIButton *button = _itemArray[i];
         if (button != btn)
         {
             if (CGRectContainsPoint(button.frame, point))
             {
                 return i;
             }
         }
     }
     return -1;
 }

  這樣,我們通過位置判斷,讓控件的位置得以改變。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved