Skip to content

Instantly share code, notes, and snippets.

@bacella
Created January 26, 2014 05:09
Show Gist options
  • Save bacella/8628689 to your computer and use it in GitHub Desktop.
Save bacella/8628689 to your computer and use it in GitHub Desktop.
Movable Button on iOS
- (void)viewDidLoad
{
[super viewDidLoad];
self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btn.frame = CGRectMake(10, 10, 50, 50);
[self.btn setTitle:@"Touch" forState:UIControlStateNormal];
[self.btn addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[self.btn addTarget:self action:@selector(dragEnded:withEvent:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
[self.view addSubview:self.btn];
}
- (void) dragMoving: (UIControl *) c withEvent:ev
{
c.center = [[[ev allTouches] anyObject] locationInView:self.view];
}
- (void) dragEnded: (UIControl *) c withEvent:ev
{
c.center = [[[ev allTouches] anyObject] locationInView:self.view];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment