TouchesMoved 只移动一点点
TouchesMoved only moves a little
我试图让用户在屏幕上拖动一个标签,但在模拟器中,每次我触摸屏幕上的某个地方时它只会移动一点点。它会跳到该位置然后轻轻拖动,但随后它会停止拖动,我必须触摸不同的位置才能使其再次移动。这是我的 .m 文件中的代码。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Drag = [[event allTouches] anyObject];
firstInitial.center = [Drag locationInView: self.view];
}
我的最终目标是能够在屏幕上拖动三个不同的标签,但我只是想先解决这个问题。如果有任何帮助,我将不胜感激!
谢谢。
尝试使用 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;
}
}
我试图让用户在屏幕上拖动一个标签,但在模拟器中,每次我触摸屏幕上的某个地方时它只会移动一点点。它会跳到该位置然后轻轻拖动,但随后它会停止拖动,我必须触摸不同的位置才能使其再次移动。这是我的 .m 文件中的代码。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Drag = [[event allTouches] anyObject];
firstInitial.center = [Drag locationInView: self.view];
}
我的最终目标是能够在屏幕上拖动三个不同的标签,但我只是想先解决这个问题。如果有任何帮助,我将不胜感激!
谢谢。
尝试使用 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;
}
}