我试图在使用核心动画时淡入一些视图。一切都很好,直到第 4 或第 5 个视图淡入。动画停止,几秒钟后,其余视图仅显示在屏幕上(没有动画发生)。我在第三代 iPad 上进行了测试,所以它不会是硬件过时的问题。代码:
viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
self.subViews = self.view.subviews;
for (UIView *view in self.subViews) {
view.backgroundColor = [UIColor clearColor];
PhotoFrame *photoFrame = [[PhotoFrame alloc] init];
photoFrame.photo.image = [UIImage imageNamed:[NSString stringWithFormat:@"test%d.jpg", arc4random() % 6 + 1]];
[view addSubview:photoFrame];
view.layer.opacity = 0;
}
}
viewDidAppear
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self startAnimations];
}
开始动画
- (void)startAnimations {
int count = 1;
for (UIView *view in self.subViews) {
CALayer *animationLayer = view.layer;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = @0.0;
animation.toValue = @1.0;
animation.duration = 0.3;
animation.beginTime = CACurrentMediaTime() + count * 0.3;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[animationLayer addAnimation:animation forKey:@"opacityAnimation"];
count++;
}
}
我希望任何人都可以帮助我。