我想给一个按钮添加一个动画,使按钮从 A 点移到 B 点。
下面的代码可以做到。但奇怪的是,当按钮移动时,它不再检测到触摸。
如果您和我点击按钮的原始位置,它可以触发按钮事件。我的代码中是否缺少任何内容?请帮我。
- (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"];
}