it is very simple - just write this method in the viewController.m file. Here the button moves to where you touch the UIView.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint pt = [[touches anyObject] locationInView:self.view]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1.0];
Now listen - if you want to move it horizontally, then fix the y coordinate and rect will be ...
CGRect rect = CGRectMake(pt.x, button.frame.origin.y, button.frame.size.height, button.frame.size.width);
and if you want to move it vertically, set the x coordinate - and rect will be
CGRect rect = CGRectMake( button.frame.origin.x, pt.y, button.frame.size.height, button.frame.size.width);
Thanks!
source share