iPhone怎么做指定路径的动画
之前做动画一直用这种方法
[UIView beginAnimations:@"animation" context:NULL]; [UIView setAnimationDuration:0.5f]; // 新位置 [UIView commitAnimations];
但是解决不了View需要转向的问题。
我现在需要让这个动画过程中,View有一个抖动的效果,这个实现不了,除非我用延迟,做两次动画,感觉这样很恶心。有没有什么办法是可以指定路径做动画的
燃烧的2B
11 years, 9 months ago
Answers
用Core Animation,可以指定动画路径。
下面例子实现一个物体下落再弹起的动画
[CATransaction begin]; [CATransaction setValue:[NSNumber numberWithFloat:ANIMATION_DURATION] forKey:kCATransactionAnimationDuration]; CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; CGMutablePathRef positionPath = CGPathCreateMutable(); CGPathMoveToPoint(positionPath, NULL, [theView layer].position.x, [theView layer].position.y); CGPathAddQuadCurveToPoint(positionPath, NULL, [theView layer].position.x, - [theView layer].position.y, [theView layer].position.x, [theView layer].position.y); CGPathAddQuadCurveToPoint(positionPath, NULL, [theView layer].position.x, - [theView layer].position.y * 1.5, [theView layer].position.x, [theView layer].position.y); CGPathAddQuadCurveToPoint(positionPath, NULL, [theView layer].position.x, - [theView layer].position.y * 1.25, [theView layer].position.x, [theView layer].position.y); positionAnimation.path = positionPath; positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; [[theView layer] addAnimation:positionAnimation forKey:@"positionAnimation"]; CGPathRelease(positionPath); [CATransaction commit];
当然,在
[CATransaction begin]; ... [CATransaction commit];
代码块中,你可以对一个或多个视图同时添加多个动画效果(如改变其颜色、大小等),来实现更为复杂的动画
菊部地区有血
answered 11 years, 9 months ago