当 UIButton 具有动画效果时,我想识别按钮上的事件[重复]

2024-01-22

可能的重复:
使用 UIView animateWithDuration 进行动画处理时,无法触摸 UIButton https://stackoverflow.com/questions/8346100/uibutton-cant-be-touched-while-animated-with-uiview-animatewithduration

我想从左到右对 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!"); }];

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


您应该使用点击手势识别器来获取该按钮的点击事件 如下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!");];

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

  - (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:如果您想找到触摸手势,您应该使用 UILongPressGestureRecognizer 而不是 UITapGestureRecognizer 并设置持续时间。

我希望它可以帮助你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当 UIButton 具有动画效果时,我想识别按钮上的事件[重复] 的相关文章

随机推荐