是否有可能成功动画移动的UIButton?

时间:2010-08-25 18:00:57

标签: iphone objective-c animation core-animation uibutton

我正在尝试动画一个在屏幕上移动的按钮。在任何时候,用户都可以按下按钮。但按钮不响应触摸。我尝试了一个动画块,但按钮只是立即移动到它的结束坐标,而框架显示动画(按钮叫做bubble):

[UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:5.0];
    CGAffineTransform newTransform = CGAffineTransformMakeScale(3, 3);
    bubble.transform = CGAffineTransformTranslate(newTransform, 0, -460);
    [UIView commitAnimations];

然后我在一些示例代码的帮助下尝试了Core Animation(路径并不重要,只是一个例子):

CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
    CGPathAddCurveToPoint(thePath,NULL,
                          15.f,250.0f,
                          295.0f,250.0f,
                          295.0f,15.0f);
    CAKeyframeAnimation *theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    theAnimation.path=thePath;
    CAAnimationGroup *theGroup = [CAAnimationGroup animation];
    theGroup.animations=[NSArray arrayWithObject:theAnimation];
    theGroup.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    theGroup.duration=15.0;
    CFRelease(thePath);
    [bubble.layer addAnimation:theGroup forKey:@"animatePosition"];

但是按钮仍然没有响应触摸。顺便说一下,我在屏幕上同时有几个“泡泡”按钮,因此同时激活多个NSTimers并不是最佳选择。

有人可以提出另一种方法吗?我是否应该为UIImageViews制作动画并使它们能够触动?或者我会遇到同样的问题吗?这让我困惑了几天,所以任何帮助都非常感激。

谢谢:)

迈克尔

4 个答案:

答案 0 :(得分:3)

在动画像UIButton这样的GUI元素时,实际位置仅在动画完成时更改。这意味着动画期间的任何触摸事件只能在按钮的起始点起作用。您可以通过访问它的presentationLayer来获取按钮的当前显示位置。您可能需要实现自己的事件处理,查看用户触摸屏幕时的位置,并将其与从presentationLayer获得的边界进行比较。

答案 1 :(得分:3)

嗯,对于任何有兴趣的人,这里的解决方案非常有效:

- (void)startMinigame {
    makeBubbles = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(generateRandomBubble) userInfo:nil repeats:YES];
    floatBubbles = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(floatBubbles) userInfo:nil repeats:YES];
}

- (void)generateRandomBubble {
    //Get a random image for the bubble
    int randomIndex = arc4random()%kNumberBubbles;
    UIImage *bubbleImage = [bubbleImages objectAtIndex:randomIndex];

    //Create a new bubble object and retrieve the UIButton
    Bubble *bubble = [[Bubble alloc]initWithImage:bubbleImage andIndex:randomIndex];
    UIButton *bubbleButton = bubble.bubbleButton;

    //Configure the button
    [bubbleButton addTarget:self action:@selector(bubbleBurst:) forControlEvents:UIControlEventTouchDown];

    //Add the bubble to the scene
    [self.view insertSubview:bubbleButton belowSubview:bathTub];
    //Add the bubble to an array of currently active bubbles:
    [self.bubbles addObject:bubble];
    [bubble release];
}

- (void)floatBubbles {
    //Move every active bubble's frame according to its speed. 
    for (int i = 0; i < [bubbles count]; ++i) {
        Bubble *bubble = [bubbles objectAtIndex:i];
        int bubbleSpeed = bubble.speed;
        CGRect oldFrame = bubble.bubbleButton.frame;
        CGRect newFrame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y - bubbleSpeed, oldFrame.size.width+0.1, oldFrame.size.height+0.1);
        bubble.bubbleButton.frame = newFrame;
        if (bubble.bubbleButton.frame.origin.y < -100.0) {
            [bubbles removeObject:bubble];
            [bubble.bubbleButton removeFromSuperview];
        }
    }
}

答案 2 :(得分:1)

尝试动画uiview的frame属性。

答案 3 :(得分:1)

对于UIKit和CoreAnimation而言,这听起来有点过于复杂。看起来你正在开发类似游戏的东西。为什么不使用全局动画计时器让我们说~60 fps并在Quartz2D中进行绘图?然后,对于每次触摸,您可以快速检查Quartz刷新之间的任何匹配。