2010-08-23 49 views
2

我想動畫一個UIButton向上移動屏幕。在任何時候用戶都可以觸摸它。但是,它在移動時似乎沒有響應觸摸,僅在其動畫的開始和結束時才響應。我想這是因爲按鈕本身不動,只是它的形象。任何想法如何我可以解決這個問題?這是我的代碼到目前爲止。謝謝!動畫移動UIButton沒有響應觸摸/水龍頭移動時

- (void)viewDidLoad { 
    [self newBubble]; 
    [super viewDidLoad]; 
} 

- (void)newBubble { 
    UIButton *bubble = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 
    bubble.frame = CGRectMake(10, 380, 50, 50); 
    UIImage *bubbleImage = [UIImage imageNamed:@"bubble.png"]; 
    [bubble setBackgroundImage:bubbleImage forState:UIControlStateNormal]; 
    bubble.imageView.contentMode = UIViewContentModeScaleAspectFit; 
    [bubble addTarget:self action:@selector(bubbleBurst:) forControlEvents:UIControlEventTouchDown]; 
    [self.view addSubview:bubble]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:5.0]; 
    CGAffineTransform newTransform = CGAffineTransformMakeScale(1.5, 1.5); 
    bubble.transform = CGAffineTransformTranslate(newTransform, 10, -200); 
    [UIView commitAnimations]; 
} 

- (IBAction)bubbleBurst:(id)sender { 
    NSLog(@"Bubble burst"); 
    UIButton *bubbleBurst = sender; 
    [bubbleBurst removeFromSuperview]; 
} 

回答

1

發佈一個類似的問題後,好了,我發現answer here

基本上,你必須使用一個NSTimer幀移動的按鈕框架。在我的例子中,我擔心會有太多的NSTimers移動每個按鈕,但最終我使用了一個單一的定時器,它通過一系列按鈕循環並逐一移動它們。

0

我敢肯定,你將不得不使用的Core Animation,如果你想按鈕,同時它是移動的觸摸做出反應,但我從來沒有這樣做,所以這是真的更尖端,然後一個答案!

+1

確實我希望你是對的。任何想法使用哪些類,但?我嘗試了CAKeyframeAnimation,移動圖層的'位置'屬性,但同樣的問題。和CABasicAnimation一樣。我會堅持下去,只是希望有人能夠早點給我指點。也許我應該使用NSTimer更新按鈕的位置?但是,這會得到相當凌亂的多個按鈕... – Smikey 2010-08-24 10:22:02

-4

我同意。我認爲問題在於你沒有使用Core Animation。我無法相信你無法爲自己弄清楚。

+0

我正在做有用的事情!研究,我的朋友...研究... – zakgottlieb 2010-08-25 00:28:55

+0

那麼,如果你需要一個iPhone應用程序,你知道哪裏來。只要它不使用核心動畫:) – Smikey 2010-08-25 07:12:17

13

+[UIView animateWithDuration:delay:options:animations:completion:]方法採用UIViewAnimationOptions參數。如果你包括UIViewAnimationOptionAllowUserInteraction,你應該可以在動畫時按下按鈕:

[UIView animateWithDuration:5.0 
         delay:0.0 
        options:UIViewAnimationOptionAllowUserInteraction 
       animations:^{ 
     CGAffineTransform newTransform = CGAffineTransformMakeScale(1.5, 1.5); 
     bubble.transform = CGAffineTransformTranslate(newTransform, 10, -200); 
    } completion:nil]; 
+0

我認爲這應該被接受。 – devxoul 2016-04-02 15:44:06