当UIButton动画时,我想识别按钮上的事件 [英] When a UIButton is animating I want to recognize a event on the button

查看:28
本文介绍了当UIButton动画时,我想识别按钮上的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
用UIView animateWithDuration动画时无法触摸UIButton

我想从左到右对UIButton进行动画处理,同时在动画中,如果用户触摸按钮,则应该向我发送事件,但是当对按钮进行动画时,则不发送事件.请帮助我,我的项目到此为止.一些开发人员建议我使用

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???

推荐答案

您应该使用tapGesture识别器将Tap事件获取到该按钮如下在 viewDidLoad 中.

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];
}

这就是按原样设置Button使用动画的代码.

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

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

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

    }

编辑:如果要查找触摸手势,则应使用UILongPressGestureRecognizer而不是UITapGestureRecognizer并设置持续时间.

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 .

这篇关于当UIButton动画时,我想识别按钮上的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆