I would like to add an animation to a button which make the button remove point A to point B.
And the follow code can do it.But the weird thing is when the button is moved,it can not detect the touch any more.
If you and if I tap the original location of the button,it can trigger the button event.Is anything missing in my code?
Please help me.
- (IBAction)button:(id)sender
{
UIButton *lineButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[lineButton addTarget:self action:@selector(press:)forControlEvents:UIControlEventTouchUpInside];
[lineButton setFrame:CGRectMake(50, 50, 50, 50)];
[self.view addSubview:lineButton];
CABasicAnimation *lineAnimation;
lineAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
[lineAnimation setRemovedOnCompletion:NO];
[lineAnimation setDuration:0.15];
[lineAnimation setFillMode:kCAFillModeForwards];
[lineAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
CGRect rect = CGRectMake(100, 200, 50, 50);
lineAnimation.fromValue = [NSValue valueWithCGPoint: CGPointMake(50, 50)];
lineAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))];
[lineAnimation setAutoreverses:NO];
[lineButton.layer addAnimation:lineAnimation forKey:@"line"];
}
You have to set the new frame and layer to your button’s frame/layer after the addAnimation method like:
Or you can simply use animation blocks (Easier to use, code looks nice, etc..) like: