3

Possible Duplicate:
UIButton can’t be touched while animated with UIView animateWithDuration

I want to animate a UIButton from left to right, while its animating if user touch the button I should be sent an event, but when the button animating it isn't send event. Please help me, my project is stoped on this point. Some developer suggested me to use

[UIView animateWithDuration:3
                      delay:0
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                                myBtn.frame=CGRectMake(0,
                                                       100,
                                                       myBtn.frame.size.width,
                                                       myBtn.frame.size.height);
                              }
                 completion:^(BOOL finished) { NSLog(@"Animation Completed!"); }];

this method but it is not working too, please tell what should I do???

Community
  • 1
  • 1
Rais
  • 95
  • 6

3 Answers3

1

You should use tapGesture recogniser for getting the Tap event to that button as below in viewDidLoad.

- (void)viewDidLoad

 {
   UITapGestureRecognizer *btnTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
   btnTapped.numberOfTapsRequired = 1;
   btnTapped.delegate = self;
   [myBtn addGestureRecognizer:btnTapped];//here your button on which you want to add sopme gesture event.
   [btnTapped release];

 [super viewDidLoad];
}

And That's Your Code for animating the Button use as it is .

[UIView animateWithDuration:3
                          delay:0
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                                    myBtn.frame=CGRectMake(0, 100, myBtn.frame.size.width, myBtn.frame.size.height);
                                 }
                     completion:^(BOOL finished) {NSLog(@"Animation Completed!");];

Below is the Delegate method for allowing simultaneous recognition

  - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
   {
      return YES;
   }

 here Above methods   Returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES


   - (void)tapAction:(UITapGestureRecognizer*)gesture
    {
     //do here which you want on tapping the Button..

    }

EDIT: If You want to find the touch gesture you should use UILongPressGestureRecognizer instead of UITapGestureRecognizer and set the duration.

I hope it may help you .

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
0

The UIButton cant be used while it's under animation, you have to use an NSTimer:

timer = [NSTimer timerWithTimeInterval:.005
                                      target:self
                                      selector:@selector(moveButton)
                                      userInfo:nil
                                      repeats:YES];
                [[NSRunLoop mainRunLoop] timer forMode:NSRunLoopCommonModes];

// you can change the speed of the button movement by editing (timerWithTimeInterval:.005) value

-(void)moveButton{
       button.center = CGPointMake(button.center.x+1,button.center.y);



 if (button.frame.origin.x>=self.view.frame.size.width ) {
        [timer invalidate];
    //The event that will stop the button animation

    }

}
Mutawe
  • 6,464
  • 3
  • 47
  • 90
0

The problem is that the button already gets the final frame and doesn't work with the current position.

@jrturton has given a good solution in this question: UIButton can't be touched while animated with UIView animateWithDuration

It basically implements the touchesBegan: method to work with the presentationLayer.

Community
  • 1
  • 1
Eiko
  • 25,601
  • 15
  • 56
  • 71