2012-10-15 43 views
3

可能重复:
UIButton can’t be touched while animated with 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!"); }]; 

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

回答

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 

    } 

} 
+0

我想动画14个按钮,所以你认为这种方式对我来说很简单。在某种程度上,它应该看起来像一列火车。 – Rais

+0

很抱歉,但我认为你必须! – Mutawe

+0

这似乎是一个肯定的地狱。 – Eiko

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

我希望它可以帮助你。

相关问题