Sri
Sri

Reputation: 454

how to make UIbutton draggable within UIView?

I need to drag and drop the button in my dash board i can drag and drop the button using the following code

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

but i need while drag and drop i need to change the positions of the buttons too

note:i wrote the code only for one button

please help me for my question

Upvotes: 5

Views: 10018

Answers (2)

Baig
Baig

Reputation: 5005

Use UIControlEventTouchDragInside and UIControlEventTouchUpInside events of UIButton like

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

You can use hbdraggablebutton control..

Upvotes: 0

HTU
HTU

Reputation: 1034

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

Upvotes: 4

Related Questions