【问题标题】:UIButton receives touchEvents at the wrong position after a CABasicAnimatonUIButton 在 CABasicAnimaton 之后在错误的位置接收到 touchEvents
【发布时间】:2012-07-09 12:48:22
【问题描述】:

我想为按钮添加动画,使按钮将 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"];
}

【问题讨论】:

    标签: objective-c ios uibutton touch cabasicanimation


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 不客气。 :) 请记住将此答案设置为正确的答案。
    【解决方案2】:

    在您的 interfacebuilder 中正确设置按钮和 setframes 的 autoresizing 属性。如果您旋转设备或模拟器,则会自动调整帧大小。那么你将失去原来的框架。

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      相关资源
      最近更新 更多