Reputation:
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
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
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':
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