3

可能重复:
使用 UIView animateWithDuration 进行动画处理时无法触摸 UIButton

我想从左到右为 UIButton 设置动画,如果用户触摸按钮我应该发送事件,但是当按钮动画时它不是发送事件。请帮助我,我的项目在这一点上停止了。一些开发人员建议我使用

[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!"); }];

这种方法,但它也不起作用,请告诉我该怎么办???

4

3 回答 3

1

您应该使用 tapGesture 识别器来获取该按钮的 Tap 事件,如下所示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];
}

这就是你的 Button 动画代码。

[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!");];

下面是允许同时识别的委托方法

  - (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..

    }

编辑:如果您想找到触摸手势,您应该使用 UILongPressGestureRecognizer 而不是 UITapGestureRecognizer 并设置持续时间。

我希望它可以帮助你。

于 2012-10-15T06:44:17.323 回答
0

在动画下UIButton不能使用,你必须使用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

    }

}
于 2012-10-15T06:34:34.977 回答
0

问题是按钮已经获得最后一帧并且不适用于当前位置。

@jrturton 在这个问题上给出了一个很好的解决方案:UIButton can't be touch while animated with UIView animateWithDuration

它基本上实现了 touchesBegan: 方法来处理presentationLayer。

于 2012-10-15T08:39:35.340 回答