【问题标题】:TouchesMoved only moves a littleTouchesMoved 只移动了一点点
【发布时间】:2015-01-10 18:20:18
【问题描述】:

我试图让用户在屏幕上拖动标签,但在模拟器中,每次我触摸屏幕上的某个位置时它只会移动一点。它会跳到该位置然后稍微拖动,但随后它会停止拖动,我必须触摸另一个位置才能让它再次移动。这是我的 .m 文件中的代码。

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

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

}

我的最终目标是能够在屏幕上拖动三个不同的标签,但我只是想先解决这个问题。我将非常感谢任何帮助!

谢谢。

【问题讨论】:

    标签: ios xcode drag touchesbegan touchesmoved


    【解决方案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;
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2017-11-25
      相关资源
      最近更新 更多