2010-08-23 61 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