2012-10-15 45 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並設置時間。

我希望它可以幫助你。

相關問題