0

I'm writing a custom UIButton that can draggable around the screen. When I press this button, a uiview will bee added into superview. I've adding pan gesture for this view so it's can draggable too. And now I wanna make both uiview and button can draggable at the same time. How can I do that?
For example:

  • If I drag the uiview or the button to a point, the other will move too. You can see the picture below for more information.
    enter image description here
2
  • 1
    set your UIView frame respect to your UIButton like UIView.center.x = UIButton.center.x + 20 like this and update it when button moves so it will always move with button. Aug 11, 2016 at 9:10
  • Son Check my code.It works fine Aug 11, 2016 at 9:36

2 Answers 2

1

I would encapsulate the button and the view in a parent transparent UIView. Add the pan gesture to it and move it around... both will then move.

1
  • I've think to this solution. But my custom button is always floating on the screen and have public method to call from others class. So as your comment, when I pressed this button I must add it into a view, isn't it? Aug 11, 2016 at 9:05
0

I tried the sample code for your application.It works perfectly.When you drag button,the view also moves simultaneously.

First I created button and UIView.I hooked up that.

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *btnDragg;
- (IBAction)actionDragg:(id)sender;
@property (strong, nonatomic) IBOutlet UIView *viewDrag;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


@synthesize btnDragg,viewDrag;

- (void)viewDidLoad 
{
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
   UIPanGestureRecognizer *panRecognizer;
   panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(wasDragged:)];
   panRecognizer.cancelsTouchesInView = YES;
   [btnDragg addGestureRecognizer:panRecognizer];
}


- (void)wasDragged:(UIPanGestureRecognizer *)recognizer
{
   UIButton *button = (UIButton *)recognizer.view;

   CGPoint translation = [recognizer translationInView:button];
   CGPoint translationView = [recognizer translationInView:viewDrag];
   button.center = CGPointMake(button.center.x + translation.x, button.center.y + translation.y);
   viewDrag.center = CGPointMake(viewDrag.center.x + translationView.x, viewDrag.center.y + translationView.y);

   [recognizer setTranslation:CGPointZero inView:button];
   [recognizer setTranslation:CGPointZero inView:viewDrag];
}

Before I drag the button,it is in original positionenter image description here

Then When I drag the button,the view also moves with button.Now it changes the position after the I drag the button.enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.