I have implemented a dragable UIButton in a ViewController.Apart from this dragable button I also have two UIButtons which are supposed to be detected when i drag a Button over them and change the title of static button.How can i achieve this?
this is how dragable button is implemented.
@interface ButtonAnimationViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *firstButton; // dragable button
@property (weak, nonatomic) IBOutlet UIButton *oneButton; //normal button
@property (weak, nonatomic) IBOutlet UIButton *twoButton; // normal button
@implementation ButtonAnimationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.firstButton];
[self.view addSubview:self.oneButton];
[self.view addSubview:self.twoButton];
UIPanGestureRecognizer *panGesture
= [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
[self.firstButton addGestureRecognizer:panGesture];
UITapGestureRecognizer *tapGesture
= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dropping:)];
tapGesture.cancelsTouchesInView = NO;
[self.oneButton addGestureRecognizer:tapGesture];
[self.twoButton addGestureRecognizer:tapGesture];
-(void)dragging:(UIPanGestureRecognizer*)panGesture {
if (panGesture.view != self.firstButton)
{
return;
}
if (panGesture.state == UIGestureRecognizerStateBegan ||
panGesture.state == UIGestureRecognizerStateChanged)
{
CGPoint delta = [panGesture translationInView:self.view];
CGPoint center = self.firstButton.center;
center.x += delta.x;
center.y += delta.y;
self.firstButton.center = center;
[panGesture setTranslation:CGPointZero inView:self.view];
}
if (panGesture.view == self.oneButton) {
// I tried this to change the button title.
NSString *buttonTitle = self.firstButton.titleLabel.text;
self.oneButton.titleLabel.text = buttonTitle;
return;
}
//if (panGesture.state == UIGestureRecognizerStateEnded) {
// self.firstButton.center = center;
//[panGesture setTranslation:CGPointZero inView:self.view];
}