Sto cercando di consentire all'utente di trascinare un'etichettaattorno allo schermo, ma nel simulatore si sposta solo un po 'ogni volta che tocco da qualche parte sullo schermo. Passerà alla posizione e quindi trascinerà leggermente, ma poi smetterà di trascinare e devo toccare un'altra posizione per farlo spostare di nuovo. Ecco il mio codice nel mio file .m.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *Drag = [[event allTouches] anyObject];
firstInitial.center = [Drag locationInView: self.view];
}
Il mio obiettivo finale è quello di poter trascinare tre diverse etichette sullo schermo, ma sto solo cercando di affrontare questo problema prima. Gradirei molto qualsiasi aiuto!
Grazie.
risposte:
1 per risposta № 1Prova a usare a UIGestureRecognizer
invece di -touchesMoved:withEvent:
. E implementando qualcosa di simile al seguente codice.
//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;
}
}