Blane Townsend
Blane Townsend

Reputation: 3048

How to drag and drop a UIButton

I have four UIButtons in my interface file. I have outlets attached to each button. I want to be able to drag and drop them across the screen. What is the best way to do this? How can I do this? please help!!!

Upvotes: 1

Views: 1923

Answers (2)

SARATH SASI
SARATH SASI

Reputation: 1415

try this u can drag and drop your button what ever you want

implement this code in ViewDidLoad

 [self.shoeimageOutlet addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
    [self.shoeimageOutlet addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragOutside];

it will call the method written below

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

Upvotes: 1

Haris
Haris

Reputation: 915

There are 3 possibilities nowadays on how to enable items – that is UIViews and UIControls – to be draggable.

  • override touchesMoved
  • add a target/selector for dragging control events
  • add a pan gesture recognizer

The complete tutorial found here !!!

Upvotes: 0

Related Questions