iOS:动画期间未调用IBAction

时间:2014-02-28 15:09:52

标签: ios objective-c uiviewanimation

在我的应用程序中,我将此方法称为移动UIButton

- (void) buttonMovement{

    [UIView animateWithDuration:0.8
                          delay:0.0
                        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         // do whatever animation you want, e.g.,
                         play_bt.center = CGPointMake(play_bt.center.x, play_bt.center.y-20);
                     }
                     completion:NULL];
}

通过这种方式,我的按钮完成了“向上和向下”运动,但如果我“推”它,IBAction就不起作用了。如果我不做这个动画它工作正常。为什么? 如果这个动画是一个调用动作的问题,那么做一个不给我问题的动画的方法是什么? 另一种方法是将按钮移动为一个图像视图,而我放了一个隐形按钮,但是我失去了印刷效果,它变成了所有噪音。

2 个答案:

答案 0 :(得分:3)

很多人在这里提供错误的信息。我试着评论一下。现在是时候发表一个确凿的答案了。引用我对上述@Radu链接的“clicking an animating image/button”问题的回答:

简短的回答是,您无法在动画制作时点按视图。原因是视图实际上并不从开始到结束位置。相反,系统使用Core Animation中的“表示层”来创建视图对象移动/旋转/缩放/外观的外观。

你需要做的是将一个水龙头手势识别器附加到一个完全包围动画(可能是整个屏幕)的包含视图,然后编写查看水龙头坐标的代码,进行坐标转换,并决定是否点按您关注的视图。如果按钮位于某个按钮上,并且您希望该按钮突出显示您还需要处理该逻辑。

我在github上有一个示例项目,它展示了当你使用CABasicAnimation进行UIView动画和核心动画时它是如何做到这一点的(动画层是动画,而不是视图。)

Core Animation demo with hit testing

答案 1 :(得分:0)

解决方法是为动画按钮实现touchesBegan而不是IBAction。

您可能需要查看此处给出的答案:Clicking an animating image / button

- (void)touchesBegan:(NSSet*) touches withEvent:(UIEvent *) event{

    // Get location of touched point
    CGPoint point = [[touches anyObject] locationInView:self.view];

    //
    // Check if point is inside your button frame
    // or compare it with the presentation layer
    //

}