Skip to content

Instantly share code, notes, and snippets.

@sebk
Created May 4, 2012 07:14
Show Gist options
  • Save sebk/2592812 to your computer and use it in GitHub Desktop.
Save sebk/2592812 to your computer and use it in GitHub Desktop.
Move component with UIPanGestureRecognizer
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"BUTTON" forState:UIControlStateNormal];
[button setFrame:CGRectMake(50, 50, 150, 150)];
[self.view addSubview:button];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(invokePanGesture:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:2];
[button addGestureRecognizer:panGesture];
}
- (void)invokePanGesture:(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:CGPointMake(0, 0) inView:self.view];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment