Déplacer UIView par rapport au toucher
je suis en train de déplacer une UIView par rapport à l'utilisateur touche.
Voici ce que j'ai en ce moment:
int oldX, oldY;
BOOL dragging;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(window.frame, touchLocation)) {
dragging = YES;
oldX = touchLocation.x;
oldY = touchLocation.y;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(window.frame, touchLocation) && dragging) {
CGRect frame;
frame.origin.x = (window.frame.origin.x + touchLocation.x - oldX);
frame.origin.y = (window.frame.origin.y + touchLocation.y - oldY);
window.frame = frame;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
dragging = NO;
}
La vue conserve le scintillement d'un endroit à un autre, et je ne sais pas quoi faire d'autre.
Toute aide appréciée.
source d'informationauteur Jack Greenhill
Vous devez vous connecter pour publier un commentaire.
Modifier le touchesBegan et touchesMoved méthodes pour être comme ce qui suit.
La touchesBegan:withEvent: méthode.
La touchesMoved:withEvent: méthode.
La touchesEnded:withEvent: méthode.
Ce que vous voulez est d'utiliser un
UIPanGestureRecognizer
introduit dans l'iOS 3.2. Vous l'utilisez avec quelque chose d'aussi facile que cela (à partir de votreUIViewController
sous-classe)://Au-dessus de réponse rapide 4.0
var glisser -: Bool = false
Voici le code à Swift, un peu plus généraliste version qui fonctionne avec une ImageView qui a été créé sur l'écran.