Pergunta

In my app I call this method to move a 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];
}

In this way my button done an "up&down" movement but if I "push" it, IBAction doesn't work. If I don't do this animation it work fine. Why? If this animation is a problem to call the action, what's the way to do an animation that don't give me problems? Another way is to move the button as an imageview and over I put an invisible button, but I lost the press effetcts, and it became all noise.

Foi útil?

Solução 2

A workaround is to implement touchesBegan instead of IBAction for your animating button.

You may want to check the answers given here: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
    //

}

Outras dicas

A lot of people are providing wrong information here. I tried to weigh in with comments. Now it's time to post a conclusive answer. To quote my answer to the "clicking an animating image/button" question linked by @Radu above:

The short answer is that you can't tap views while they are animating. The reason is that the views don't actually travel from the start to the end location. Instead, the system uses a "presentation layer" in Core Animation to create the appearance of your view object moving/rotating/scaling/whatever.

What you have to do is attach a tap gesture recognizer to a containing view that completely encloses the animation (maybe the entire screen) and then write code that looks at the coordinates of the tap, does coordinate conversion, and decides if the tap is on a view that you care about. If the tap is on a button and you want the button to highlight you'll need to handle that logic too.

I have a sample project on github that shows how to do this when you are doing both UIView animation and Core Animation with CABasicAnimation (which animates layers, not views.)

Core Animation demo with hit testing

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top