我试图允许用户在屏幕上拖动标签,但在模拟器中,每次触摸屏幕上的某个位置时它只会移动一点。它将跳转到该位置,然后稍微拖动,但随后它将停止拖动,我必须触摸另一个位置以使其再次移动。这是我的.m文件中的代码。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Drag = [[event allTouches] anyObject];
firstInitial.center = [Drag locationInView: self.view];
}
我的最终目标是能够在屏幕上拖动三个不同的标签,但我只是想先解决这个问题。我非常感谢任何帮助!
感谢。
答案 0 :(得分: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;
}
}