user5226797
user5226797

Reputation:

floating button which can be dragged to anywhere in the view controller?

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

Upvotes: 1

Views: 3266

Answers (2)

user3820674
user3820674

Reputation: 262

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.

Upvotes: 1

Alex
Alex

Reputation: 3981

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.

Upvotes: 1

Related Questions