2010-12-11 53 views
5

J'ai besoin de faire glisser le bouton dans mon tableau de bord que je peux faire glisser et déposer le bouton en utilisant le code suivantcomment rendre UIbutton draggable dans UIView?

- (void)viewDidLoad { 
     [super viewDidLoad]; 
    [eventButton addTarget:self action:@selector(draggedOut:withEvent:)forControlEvents:UIControlEventTouchDragOutside |UIControlEventTouchDragInside]; 
    } 

- (void) draggedOut: (UIControl *) c withEvent: (UIEvent *) ev { 
     CGPoint point = [[[ev allTouches] anyObject] locationInView:self.view]; 
    if(point.y > 22 && point.y <300) 
      c.center = CGPointMake(237, point.y - c.bounds.size.height/2 -5); 
    } 

mais j'ai besoin tout en glisser-déposer i besoin de changer les positions des boutons trop

Note: i a écrit le code pour un seul bouton

s'il vous plaît aidez-moi à ma question

Répondre

4
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [eventButton addTarget:self action:@selector(dragOut:withEvent:)forControlEvents:UIControlEventTouchDragOutside |UIControlEventTouchDragInside]; 
    } 

-(IBAction)dragOut: (id)sender withEvent: (UIEvent *) event { 
UIButton *selected = (UIButton *)sender; 
selected.center = [[[event allTouches] anyObject] locationInView:self.view]; 
} 
0

utilisation UIControlEventTouchDragIn côté et événements UIControlEventTouchUpInside 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 ..