我想在Android中创建一个类似QuickActionBar的视图,当点击左上角的按钮时,它会打开,但是子视图看起来好像来自右边,左边墙上打了一针,然后反弹回来,然后驻扎。 视图正在显示,但我无法为子视图设置动画..
答案 0 :(得分: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"];
}
答案 1 :(得分: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) {
}];
}
}];
}
干杯。