2012-12-03 5 views
1

Je dois glisser et déposer l'uibutton dans la sous-vue seulement. En fait, je prends subview dans la vue principale. UIbutton est ajouter dans la sous-vue. Lorsque je fais glisser le bouton, il se déplace sur les deux vues, mais je dois faire glisser le bouton uniquement dans la sous-vue. Quelqu'un peut-il m'aider s'il-vous-plaît. Merci d'avance.Comment rendre UIButton Dragable?

- (void) drag 
{ 
    //subview 
    UIView *viewscramble = [[UIView alloc] initWithFrame:CGRectMake(0, 90, 320, 50)]; 
    [viewscramble setBackgroundColor:[UIColor redColor]]; 
    [self.view addSubview:viewscramble]; 

    //button 
    UIButton *btnscramble = [[UIButton alloc] initWithFrame:CGRectMake(5, 0, 50, 50)]; 
    btnscramble.titleLabel.text = @"A"; 
    [btnscramble setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [btnscramble addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
    [btnscramble setBackgroundImage:[UIImage imageNamed:@"box.png"] forState:UIControlStateNormal]; 

    //add button`  
    [viewscramble addSubview:btnscramble]; 
    [btnscramble release]; 
} 

- (IBAction)imageMoved:(id)sender withEvent:(UIEvent *) event 
{ 
    CGPoint point = [[[event allTouches]anyObject] locationInView:viewscramble]; 
    UIControl *control = sender; 
    control.center = point; 
} 
+1

peut vous envoyer un code ?? s'il vous plaît –

+0

ajouter du code :) – Rushabh

+0

@Angel créer globalement signifie viewscramble dans le fichier .h juste définir de sorte que vous pouvez l'utiliser dans toute la classe ... –

Répondre

0
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint pointMoved = [touch locationInView:"SUBVIEWNAME.view"];   
    button.frame = CGRectMake(pointMoved.x, pointMoved.y, 64, 64); 
} 
3

je viens de trouver la solution de votre question de: - http://www.cocoanetics.com/2010/11/draggable-buttons-labels/

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // create a new button 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setTitle:@"Drag me!" forState:UIControlStateNormal]; 

    // add drag listener 
    [button addTarget:self action:@selector(wasDragged:withEvent:) 
     forControlEvents:UIControlEventTouchDragInside]; 

    // center and size 
    button.frame = CGRectMake((self.view.bounds.size.width - 100)/2.0, 
      (self.view.bounds.size.height - 50)/2.0, 
      100, 50); 

    // add it, centered 
    [self.view addSubview:button]; 
} 


- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event 
{ 
    // get the touch 
    UITouch *touch = [[event touchesForView:button] anyObject]; 

    // get delta 
    CGPoint previousLocation = [touch previousLocationInView:button]; 
    CGPoint location = [touch locationInView:button]; 
    CGFloat delta_x = location.x - previousLocation.x; 
    CGFloat delta_y = location.y - previousLocation.y; 

    // move button 
    button.center = CGPointMake(button.center.x + delta_x, 
     button.center.y + delta_y); 
} 

Espérons son utile pour vous :)

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

    UITouch *tap = [touches anyObject]; 
    CGPoint pointToMove = [tap locationInView:yourSubView]; // just assign subview where you want to move the Button 
} 
+0

Cela ne fonctionne pas. – Abha

+0

@Angel crée globalement des vues brouillées dans le fichier .h juste défini pour que vous puissiez l'utiliser en classe entière ... –

+0

- [UIButton anyObject]: sélecteur non reconnu envoyé à l'instance 0x758a930 – Abha

0
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    if([touch view] == YOUR_BUTTON_OBJECT){ 
     CGPoint point = [touch locationInView:YOUR_PARENT_VIEW]; 
     //YOUR_X_LIMIT is your subview end in x direction. 
     //YOUR_Y_LIMIT is your subview end in y direction. 
     if(point.x < YOUR_X_LIMIT && point.y < YOUR_Y_LIMIT){ 
      button.center = point; 
     }else{ 
     // do nothing since touch is outside your limit. 
     } 
    } 
} 
0

Utilisez UIControlEventTouchDragInside et UIControlEventTouchUpInside un événement s de UIButton comme

// add drag listener 
[button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
// add tap listener 
[button addTarget:self action:@selector(wasTapped:) forControlEvents:UIControlEventTouchUpInside]; 

Vous pouvez utiliser le contrôle hbdraggablebutton ..