iPhone中想删除某张照片时,点击删除键,就能看到照片被回收到垃圾箱的动画。
今天就来模拟一下这个动画(据说有个私有API可以实现,不过私有的嘛,忽略之)。
首先仔细观察下这个动画,包含了位置,大小还有可见三个主要动画。
为了清楚的说明,先上核心代码:
[代码]c#/cpp/oc代码:
01 |
UIBezierPath
*movePath = [UIBezierPath bezierPath]; |
02 |
[movePath
moveToPoint:fromPoint]; |
04 |
[movePath
addQuadCurveToPoint:toPoint |
05 |
controlPoint:CGPointMake(toPoint.x,fromPoint.y)]; |
08 |
CAKeyframeAnimation
*moveAnim = [CAKeyframeAnimation animationWithKeyPath: @"position" ]; |
09 |
moveAnim.path
= movePath.CGPath; |
10 |
moveAnim.removedOnCompletion
= YES; |
12 |
CABasicAnimation
*scaleAnim = [CABasicAnimation animationWithKeyPath: @"transform" ]; |
13 |
scaleAnim.fromValue
= [NSValue valueWithCATransform3D:CATransform3DIdentity]; |
14 |
scaleAnim.toValue
= [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]; |
15 |
scaleAnim.removedOnCompletion
= YES; |
17 |
CABasicAnimation
*opacityAnim = [CABasicAnimation animationWithKeyPath: @"alpha" ]; |
18 |
opacityAnim.fromValue
= [NSNumber numberWithFloat:1.0]; |
19 |
opacityAnim.toValue
= [NSNumber numberWithFloat:0.1]; |
20 |
opacityAnim.removedOnCompletion
= YES; |
22 |
CAAnimationGroup
*animGroup = [CAAnimationGroup animation]; |
23 |
animGroup.animations
= [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim, nil]; |
24 |
animGroup.duration
= 1; |
25 |
[imageView.layer
addAnimation:animGroup forKey:nil]; |
UIBezierPath是用来创建各种曲线的类,这个类很强大,各种你能想到的都可以用它来完成。
这里我们建立的二次曲线实际上就是从照片的中心点位置到垃圾箱终点的一条曲线。
至于函数中controlPoint的选取,自己查阅API吧,这里就不多说
接着我们建立了一个CAKeyframeAnimation的动画,这个主要用于实现动画的轨迹变化,我们将动画的path值设为之前定义的曲线值。
这样动画就会按我们设定的轨迹移动了。
接下来是大小变化的动画,设定了最初和最终的画面大小。CATransform3DMakeScale是用于生成变换矩阵的东东,对于二维的,z值始终为1.
紧接着是生成透明度的动画,很好理解。
由于我们用到了三种动画,所以需要用CAAnimationGroup,这样一次性的使用它们。
这样我们就完成了这样的动画,试试吧。