Created
May 4, 2012 07:14
-
-
Save sebk/2592812 to your computer and use it in GitHub Desktop.
Move component with UIPanGestureRecognizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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