当一个UIButton的动画我想在按钮识别事件[复制](When a UIButton is ani

2019-09-24 03:18发布

可能重复:
而与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!"); }];

这种方法,但它是不是工作压力太大,请告诉我该怎么办???

Answer 1:

您应该使用tapGesture识别器在获取点击事件到按钮,如下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];
}

那就是你的动画按钮使用,因为它是代码。

[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并设置时间。

我希望它可以帮助你。



Answer 2:

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

    }

}


Answer 3:

问题是,该按钮已经得到最后一帧,并且不与当前位置的工作。

@jrturton给了这个问题一个很好的解决方案: UIButton的不能碰,而与UIView的animateWithDuration动画

它基本上实现了的touchesBegan:方法与表示层的工作。



文章来源: When a UIButton is animating I want to recognize a event on the button [duplicate]