2015-01-10 42 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; 

    } 

} 
相关问题