2010-12-20 2 views
14

J'essaie de déplacer un UIView par rapport aux touches de l'utilisateur.Déplacer UIView par rapport au toucher

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 continue vacillante d'un endroit à l'autre, et je ne sais pas quoi faire d'autre.

Toute aide appréciée.

Répondre

12

Modifier les touchesBegan et touchesModules pour ressembler à ce qui suit.

float oldX, oldY; 
BOOL dragging; 

Le touchesBegan: Méthode: withEvent.

- (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; 
    } 
} 

Le touchesMoved: Méthode: withEvent.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    if (dragging) { 

     CGRect frame = window.frame; 
     frame.origin.x = window.frame.origin.x + touchLocation.x - oldX; 
     frame.origin.y = window.frame.origin.y + touchLocation.y - oldY; 
     window.frame = frame; 
    } 
} 

Le touchesEnded: Méthode: withEvent.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    dragging = NO; 
} 
+0

fenêtre est l'objet que vous faites glisser? – EmptyStack

+0

Oui, il s'agit d'une vue créée dans Interface Builder. –

+0

C'est beaucoup mieux, mais bizarrement zoom sur le côté chaque fois qu'il est touché. –

62

Ce que vous voulez, c'est utiliser un UIPanGestureRecognizer, introduit dans iOS 3.2. Vous pouvez l'utiliser avec quelque chose d'aussi simple que cela (de votre UIViewController sous-classe):

-(void)viewDidLoad; 
{ 
    [super viewDidLoad]; 
    UIPanGestureRecognizer* pgr = [[UIPanGestureRecognizer alloc] 
             initWithTarget:self 
               action:@selector(handlePan:)]; 
    [self.panningView addGestureRecognizer:pgr]; 
    [pgr release]; 
} 

-(void)handlePan:(UIPanGestureRecognizer*)pgr; 
{ 
    if (pgr.state == UIGestureRecognizerStateChanged) { 
     CGPoint center = pgr.view.center; 
     CGPoint translation = [pgr translationInView:pgr.view]; 
     center = CGPointMake(center.x + translation.x, 
          center.y + translation.y); 
     pgr.view.center = center; 
     [pgr setTranslation:CGPointZero inView:pgr.view]; 
    } 
} 
+1

Beaucoup plus fluide défilement !!! – user523234

+3

Cette réponse est meilleure que la réponse acceptée! – Rick

+1

Si vous visualisez est une UIImageview, assurez-vous de userInteractionEnabled à YES comme: _imageView.userInteractionEnabled = YES; – ddiego

0

Code est ici à Swift, une version légèrement plus généraliste qui fonctionne avec un ImageView qui a été créé à l'écran.

let panGestureRecongnizer = UIPanGestureRecognizer(target:self, action: "handlepan:") 
imageView.addGestureRecognizer(panGestureRecongnizer) 

func handlepan(sender: UIPanGestureRecognizer) { 
    if (sender.state == UIGestureRecognizerState.Changed) { 
     var center = sender.view?.center 
     let translation = sender.translationInView(sender.view) 
     center = CGPointMake(center!.x + translation.x, center!.y + translation.y) 
     sender.view?.center = center! 
     sender .setTranslation(CGPointZero, inView: sender.view) 
    } 
} 
Questions connexes