0

我想在 Android 中创建一个类似于 QuickActionBar 的视图,当单击左上角的按钮时,它会简单地打开,但它的子视图看起来好像它们来自右侧,撞到左墙上,然后反弹回来,然后驻扎. 视图正在出现,但我无法为子视图设置动画..

4

2 回答 2

0

CAKeyframeAnimation 看起来适合您的问题。我之前为标签在屏幕上滑动编写了此动画功能。为了您的目的,我稍微更新了 xValues,但很可能您将不得不使用它们。此外,在撞墙后,您可能想要使用 scale.y 值,您可以轻松地将其集成到关键帧动画中。

+ (void) slideAcrossScreen:(UIView *) view Duration:(CGFloat) duration FromLeft:(BOOL) fromLeft
{
    //This is to make it work with both left-right-left and right-left-right anims.
    CGFloat slideLength = fromLeft ? -view.frame.origin.x : [[UIScreen mainScreen] bounds].size.width - (view.frame.origin.x + view.frame.size.width);

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];

    NSArray *xValues =
              @[[NSNumber numberWithFloat:view.bounds.origin.x],
                [NSNumber numberWithFloat:view.bounds.origin.x + 10 * slideLength / 10],
                [NSNumber numberWithFloat:view.bounds.origin.x + 10 * slideLength / 10],
                [NSNumber numberWithFloat:view.bounds.origin.x + 9 * slideLength / 10]];

    [anim setValues:xValues];

    NSArray *timeFrames = 
              @[[NSNumber numberWithFloat:0.0],
                [NSNumber numberWithFloat:0.85],
                [NSNumber numberWithFloat:0.9],
                [NSNumber numberWithFloat:1.0]];

    [anim setKeyTimes:timeFrames];
    [anim setDuration:duration];
    [anim setFillMode:kCAFillModeForwards];
    [anim setRemovedOnCompletion:FALSE];

    [view.layer addAnimation:anim forKey:@"slide"];
}
于 2013-04-22T13:51:30.510 回答
0

我只花了 15 分钟来解决您的问题,因为动画视图总是很有趣的部分。所以这里是片段,你把你的传递view给它,它就会动画化。当然,如果你愿意,你可以添加更多。

+ (void) animateView : (UIView *) view{
    __block CGRect frame = view.frame;
    frame.origin.x = - 320;
    [view setFrame:frame];
    [UIView animateWithDuration:1.5f animations:^{
        frame.origin.x = 10.0f;
        frame.size.width = 310.0f;
        [view setFrame:frame];
    } completion:^(BOOL finished) {
        if (finished) {
            [UIView animateWithDuration:0.5 animations:^{
                frame.origin.x = 0.0f;
                frame.size.width = 320.0f;
                [view setFrame:frame];
            } completion:^(BOOL finished) {
            }];
        }
    }];
}

干杯。

于 2013-04-22T15:21:44.820 回答