什么是模仿iPhone上UIAlertView的弹跳动画的最佳方式?这有一些内置机制吗? UIAlertView本身不能满足我的需求。
我查看了动画曲线,但据我所知,他们提供的只有easyIn,easeOut和linear。
答案 0 :(得分:62)
UIAlertView使用更复杂的动画:
以下是使用CAKeyFrameAnimation
:
view.alpha = 0;
[UIView animateWithDuration:0.1 animations:^{view.alpha = 1.0;}];
CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
bounceAnimation.values = @[@0.01f, @1.1f, @0.8f, @1.0f];
bounceAnimation.keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
bounceAnimation.duration = 0.4;
[view.layer addAnimation:bounceAnimation forKey:@"bounce"];
答案 1 :(得分:30)
我调查了如何通过调动UIAlertView
将动画添加到-[CALayer addAnimation:forKey:]
的图层中。以下是我为其执行的缩放变换动画获得的值:
0.01f -> 1.10f -> 0.90f -> 1.00f
持续时间
0.2s, 0.1s, 0.1s
。
所有动画都使用缓入/缓出计时功能。这是一个CAKeyframeAnimation
,它封装了这个逻辑:
CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
bounceAnimation.fillMode = kCAFillModeBoth;
bounceAnimation.removedOnCompletion = YES;
bounceAnimation.duration = 0.4;
bounceAnimation.values = @[
[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 0.01f)],
[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.1f)],
[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 0.9f)],
[NSValue valueWithCATransform3D:CATransform3DIdentity]];
bounceAnimation.keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
bounceAnimation.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
我相信UIAlertView
在变换动画的总持续时间(0.0f
)内也会执行从1.0f
到0.4
的简单不透明度动画。
答案 2 :(得分:3)
你可以使用2个动画,一个弹出非常大,另一个重新缩放回正常大小。
(这是UIAlertView
内部使用的方法。)
或者,您可以使用较低级CAAnimation
并使用+[CAMediaTimingFunction functionWithControlPoints::::]
制作自己的曲线。
答案 3 :(得分:0)
以下是我为我正在开发的应用做的事情。当你按下视图时,我想要的效果就是弹跳。尝试使用适合您口味的值和所需的效果速度。
- (void) bounceView:(UIView*)bouncer
{
// set duration to whatever you want
float duration = 1.25;
// use a consistent frame rate for smooth animation.
// experiment to your taste
float numSteps = 15 * duration;
// scale the image up and down, halving the distance each time
[UIView animateKeyframesWithDuration:duration
delay:0
options:UIViewKeyframeAnimationOptionCalculationModeCubic
animations:^{
float minScale = 0.50f; // minimum amount of shrink
float maxScale = 1.75f; // maximum amount of grow
for(int i = 0; i< numSteps*2; i+=2)
{
// bounce down
[UIView addKeyframeWithRelativeStartTime:duration/numSteps * i
relativeDuration:duration/numSteps
animations:^{
bouncer.layer.transform = CATransform3DMakeScale(minScale, minScale, 1);
}];
// bounce up
[UIView addKeyframeWithRelativeStartTime:duration/numSteps * (i+1)
relativeDuration:duration/numSteps
animations:^{
bouncer.layer.transform = CATransform3DMakeScale(maxScale, maxScale, 1);
}];
// cut min scale halfway to identity
minScale = minScale + (1.0f - minScale) / 2.0f;
// cut max scale halfway to identity
maxScale = 1.0f + (maxScale - 1.0f) / 2.0f;
}
} completion:^(BOOL finished) {
// quickly smooth out any rounding errors
[UIView animateWithDuration:0.5*duration/numSteps animations:^{
bouncer.layer.transform = CATransform3DIdentity;
}];
}];
}