1

Saw many questions to create a floating button,which worked perfectly.But is it possible to drag the button so that user can keep it anywhere he wants?

If any one have used "flipkart" app there you can see the "ping" button.That is what i am looking for.Any one any ideas?

Thanks in advance

  • 2
    What did you try? Where does your code crash? – Helge Becker Oct 30 '15 at 08:26
  • 1
    Post your code. But here is an idea: add pan gesture, on pan apply affine translation transformation, set the pan gesture translation in the view back to zero – Nikolay Nankov Oct 30 '15 at 08:42
  • https://www.cocoacontrols.com/controls/vcfloatingactionbutton. i used this control.But here we cannot move or drag the button.Can the user move the button by dragging it .? –  Oct 30 '15 at 08:52

2 Answers2

1

Yes it is possible, you can drag around UIButtons and UIViews in iOS. Here are some posts with explanations how, that I found when I googled 'drag uibutton ios':

Basic Drag and Drop in iOS

how to make UIbutton draggable within UIView?

Touch and drag a UIButton around, but don't trigger it when releasing the finger

If you want to save the location it is dragged to by the user upon restarting the app, then use NSUserDefaults.

Community
  • 1
  • 1
Alex
  • 3,861
  • 5
  • 28
  • 44
1

You can use UIPanGestureRecognizer like so:

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) UIView *snapshotView;

@property (weak, nonatomic) IBOutlet UIButton *moveMeButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.moveMeButton addGestureRecognizer:recognizer];
}

- (void)handlePan:(UIPanGestureRecognizer*)recognizer {

    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                     recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:self.view];
}

@end


Cheers.

user3820674
  • 262
  • 1
  • 6