动画移动UIButton在移动时没有响应触摸/轻击

时间:2010-08-23 12:28:17

标签: iphone objective-c animation 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];
}

4 个答案:

答案 0 :(得分: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];

答案 1 :(得分:1)

在发布类似问题之后,我找到了answer here

基本上,您必须使用NSTimer逐帧移动按钮。在我的情况下,我担心会有太多的NSTimers涉及移动每个按钮,但最后我使用了一个单独的计时器,它循环通过一系列按钮并逐个移动它们。

答案 2 :(得分:0)

我很确定如果你想让按钮在移动时响应触摸,你必须使用Core Animation,但是我从来没有这样做过,所以这真的是一个小贴士而不是答案!

答案 3 :(得分:-4)

我同意。我认为问题在于你没有使用Core Animation。我无法相信你无法为自己解决这个问题。

相关问题