我使用CALayer
,CAReplicatorLayer
和CABasicAnimation
为条形图设置动画。条形图由向上的"段组成。我将其呈现为带有CALayer
的小矩形。然后我将它们作为子层添加到循环中的CAReplicatorLayer
。最后,我为每个小矩形添加一个动画,将矩形放入图表中的位置。
这是动画的最终状态:
这就是动画中间的样子:
以下是实现这一目标的代码:
#define BLOCK_HEIGHT 6.0f
#define BLOCK_WIDTH 8.0f
@interface TCViewController ()
@property (nonatomic, strong) NSMutableArray * blockLayers; // blocks that fall from the sky
@property (nonatomic, strong) NSMutableArray * replicatorLayers; // replicator layers that will replicate the blocks into position
@end
@implementation TCViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
self.blockLayers = [NSMutableArray array];
self.replicatorLayers = [NSMutableArray array];
NSArray * dataBlocksData = @[@1,@2,@3,@1,@5,@2,@11,@14,@5,@12,@10,@3,@7,@6,@3,@4,@1];
__block CGFloat currentXPosition = 0.0f;
[dataBlocksData enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
currentXPosition += BLOCK_WIDTH + 2.0f;
if (obj.integerValue == 0) return;
// replicator layer for data blocks in a column
CAReplicatorLayer * replicatorLayer = [[CAReplicatorLayer alloc] init];
replicatorLayer.frame = CGRectMake(currentXPosition, 0.0f, BLOCK_WIDTH, 200.0f);
// single data block layer (must be new for each replicator layer. can't reuse existing layer)
CALayer * blockLayer = [[CALayer alloc] init];
blockLayer.frame = CGRectMake(0, 200-BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
blockLayer.backgroundColor = [UIColor whiteColor].CGColor;
[replicatorLayer addSublayer:blockLayer];
replicatorLayer.instanceCount = obj.integerValue;
replicatorLayer.instanceDelay = 0.1f;
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(0, -BLOCK_HEIGHT, 0); // negative height moves back up the column
[self.blockLayers addObject:blockLayer];
[self.replicatorLayers addObject:replicatorLayer];
}];
}
- (void)viewDidLayoutSubviews {
// add replicator layers as sublayers
[self.replicatorLayers enumerateObjectsUsingBlock:^(CAReplicatorLayer * obj, NSUInteger idx, BOOL *stop) {
[self.view.layer addSublayer:obj];
}];
}
- (void)viewDidAppear:(BOOL)animated {
// add animations to each block layer
[self.blockLayers enumerateObjectsUsingBlock:^(CALayer * obj, NSUInteger idx, BOOL *stop) {
// position animation
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
animation.fromValue = @(-300);
animation.toValue = @(0);
animation.duration = 1.0f;
animation.fillMode = kCAFillModeBoth;
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
[obj addAnimation:animation forKey:@"falling-block-animation"];
}];
}
最后,抓住了这个问题。如果您未指定timingFunction
,Linear
功能或EaseIn
功能,则动画无法完全完成。像积木一样,不要完全陷入困境。看起来他们接近完成但不完全。只需一两帧。在-viewDidAppear:
中交换计时功能分配,你会发现这种奇怪现象:
来吧,试一试。我甚至尝试使用控制点(link)指定一个与EaseIn
函数完全相同的函数:
// use control points for an EaseIn timing:
animation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.42 :0.00 :1.0 :1.0];
// or simply specify the ease in timing function:
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
由于某种原因,EaseOut
和Default
计时功能工作正常,但是在样式功能方面看起来需要更长时间才能完成,可能是因为插值曲线的长度?
我尝试过很多方法来缩小这个问题:
-viewDidAppear
中。如果动画是在视图的生命周期中更早或更晚添加的,那么似乎没有什么区别。我真的想要使用EaseIn
计时功能,因为它是适合某些东西的最佳动画。我也想使用CAReplicatorLayer
,因为它完全符合我的要求。
那么EaseIn
计时功能的错误与我使用它们有什么关系?这是一个错误吗?
答案 0 :(得分:1)
我同意这似乎是CAReplicatorLayer和kCAMediaTimingFunctionEaseIn计时功能组合中的一个错误。
作为一种解决方法,我建议使用具有三个关键帧的关键帧动画。前两个关键帧通过简易时间定义您想要的动画。最后一个关键帧被定义为与第二个关键帧相同,但为动画提供了额外的时间来坐在最终的关键帧上,这似乎让复制器层为最后一个复制层设置了最终状态。
我的建议是:
CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
// extra keyframe duplicating final state
animation.values = @[@-300.0f, @0.0f, @0.0f];
// double length of original animation, with last half containing no actual animation
animation.keyTimes = @[@0.0, @0.5, @1.0];
animation.duration = 2.0f;
animation.fillMode = kCAFillModeBoth;
animation.removedOnCompletion = NO;
// ease-in for first half of animation; default after
animation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[obj addAnimation:animation forKey:@"falling-block-animation"];