2015-01-10 40 views
0

我想讓用戶在屏幕上拖動標籤,但在模擬器中,每次觸摸屏幕上的某個位置時它只會移動一點點。它會跳到這個位置,然後稍微拖動,但它會停止拖動,我必須觸摸另一個位置才能讓它再次移動。這是我的.m文件中的代碼。TouchesMoved只移動一點

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
UITouch *Drag = [[event allTouches] anyObject]; 

    firstInitial.center = [Drag locationInView: self.view]; 

} 

我的最終目標是能夠在屏幕上拖動三個不同的標籤,但我只是試圖先解決這個問題。我將不勝感激任何幫助!

謝謝。

回答

1

嘗試使用UIGestureRecognizer而不是-touchesMoved:withEvent:。並執行類似於下面的代碼。

//Inside viewDidLoad 
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(dragonMoved:)]; 
panGesture.minimumNumberOfTouches = 1; 
[self addGestureRecognizer:panGesture]; 
//********** 

- (void)dragonMoved:(UIPanGestureRecognizer *)gesture{ 

    CGPoint touchLocation = [gesture locationInView:self]; 
    static UIView *currentDragObject; 

    if(UIGestureRecognizerStateBegan == gesture.state){ 

     for(DragObect *dragView in self.dragObjects){ 

      if(CGRectContainsPoint(dragView.frame, touchLocation)){ 

       currentDragObject = dragView; 
       break; 
      } 
     } 


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

     currentDragObject.center = touchLocation; 

    }else if (UIGestureRecognizerStateEnded == gesture.state){ 

     currentDragObject = nil; 

    } 

} 
相關問題