I need to add an animation to UIButton – simple moving animation. When I do this, the button moves, but the click area of that button remains at old position:
UIButton *slideButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[slideButton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[slideButton setFrame:CGRectMake(50, 50, 50, 50)];
[self.view addSubview:slideButton];
CABasicAnimation *slideAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
[slideAnimation setRemovedOnCompletion:NO];
[slideAnimation setDuration:1];
[slideAnimation setFillMode:kCAFillModeBoth];
[slideAnimation setAutoreverses:NO];
slideAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
slideAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
[slideButton.layer addAnimation:slideAnimation forKey:@"slide"];
I already saw an answer to change the frame of the button at the end of the animation (like here UIButton receives touchEvents at the wrong position after a CABasicAnimaton) But it’s not my case because I should give possibility to user to click on the button while it’s moving… Also, I can’t use another approaches (like NSTimer) as a replacement for CABasicAnimation because I will have a complex path to move the object…
Thanks for Your help!
Well, I found that it’s not really possibly to do with CABasicAnimation. Coz animation cycle animates a layer object inside the view, but the view itself remains unchangeable, no matter what properties has been set to CABasicAnimation object.
After a few hours of googling I found a cool feature developed by MICHAEL TYSON:
http://atastypixel.com/blog/key-path-based-property-animation/
I improved it a little bit – added: autoreverse, repeatCount, cumulative, timeOffset, byValue.. And voila! Almost the same CABasicAnimation – but able to animate frame!