弹跳按钮

时间:2012-12-11 18:27:47

标签: iphone ios uibutton

我没有在互联网上找到任何解决方案,所以我决定在这里提出我的问题。有一个很酷的动画效果,你可以用一个按钮,如果你点击它跳跃和落地像一个流行音乐。如果你明白我的意思。我只是意味着如果你一次撞到地板上,按钮就像橡皮圈一样反弹。我在许多不同的游戏应用上看到了这种效果,并希望在我的应用中执行此操作。它实际上就像制作按钮更大,如果它被敲击则立即变小。有人知道怎么做这个吗?

提前致谢。

1 个答案:

答案 0 :(得分:2)

检查this blog,其中介绍了图标的反弹效果。这应该指导你正确的方向。还有check this lantean blogthis cocoanetics blog.

这是他正在使用的代码,

+ (CAKeyframeAnimation*)dockBounceAnimationWithViewHeight:(CGFloat)viewHeight
{
    NSUInteger const kNumFactors    = 22;
    CGFloat const kFactorsPerSec    = 30.0f;
    CGFloat const kFactorsMaxValue  = 128.0f;
    CGFloat factors[kNumFactors]    = {0,  60, 83, 100, 114, 124, 128, 128, 124, 114, 100, 83, 60, 32, 0, 0, 18, 28, 32, 28, 18, 0};

    NSMutableArray* transforms = [NSMutableArray array];

    for(NSUInteger i = 0; i < kNumFactors; i++)
    {
        CGFloat positionOffset  = factors[i] / kFactorsMaxValue * viewHeight;
        CATransform3D transform = CATransform3DMakeTranslation(0.0f, -positionOffset, 0.0f);

        [transforms addObject:[NSValue valueWithCATransform3D:transform]];
    }

    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.repeatCount           = 1;
    animation.duration              = kNumFactors * 1.0f/kFactorsPerSec;
    animation.fillMode              = kCAFillModeForwards;
    animation.values                = transforms;
    animation.removedOnCompletion   = YES; // final stage is equal to starting stage
    animation.autoreverses          = NO;

    return animation;
}

- (void)bounce:(float)bounceFactor
{
    CGFloat midHeight = self.frame.size.height * bounceFactor;
    CAKeyframeAnimation* animation = [[self class] dockBounceAnimationWithViewHeight:midHeight];
    [self.layer addAnimation:animation forKey:@"bouncing"];
}