objective-c - UIButton 在 CABasicAnimaton 之后在错误的位置接收到 touchEvents

标签 objective-c ios uibutton touch cabasicanimation

我想给按钮添加一个动画,使按钮从 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"];
}

最佳答案

您必须在 addAnimation 方法之后将新的框架和层设置为按钮的框架/层,例如:

lineButton.frame = rect
lineButton.layer.frame = rect

或者您可以简单地使用动画 block (更易于使用,代码看起来不错,等等),例如:

[UIView animateWithDuration:0.15 animations:^{
  lineButton.frame = CGRectMake(100, 200, 50, 50);
}];

关于objective-c - UIButton 在 CABasicAnimaton 之后在错误的位置接收到 touchEvents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11395335/

相关文章:

ios - rxSwift 按钮按下观察者问题

ios - 如何在 Swift 中使用自定义图像作为 UIButton

ios - 在 View Controller 之间传递数据

ios - 以编程方式创建 View 而不是使用界面构建器

ios - Xcode/iOS 自动委托(delegate)分配...一个错误?

ios - Twitter/Fabric 登录按钮只能工作一次

ios - NSFetchedResultsControllerDelegate 与 NSFetchedResultsController

iphone - 如何更改 UIButton 标题?

ios - 在回调中通过引用传递

ios - POST NSMutableURLRequest 中出现 405 错误