"Glitch" 在 UIView 介绍 CAAnimation 期间

"Glitch" during UIView Intro CAAnimation

我正在尝试将动画添加到 UIView。动画的 objective 是 "animate" 视图出现在屏幕上(而不是仅仅出现在那里)。

动画全尺寸缩放:从 5% 开始,增加到 120%,然后很快回到常规比例的 100%。

我的问题是全尺寸 UIView 在动画开始之前出现得非常快。

代码如下:

UIView * myView = [[UIView alloc] initWithFrame:someFrame];
[self.view addSubview:myView];
[self initialAnimationFor:myView];

-(void) initialAnimationFor:(UIView*)pView {

    const CFTimeInterval firstDuration = 0.75f;
    const CFTimeInterval secondDuration = 0.025f;
    const float initialValue = 0.05f;
    const float middleValue = 1.20f;

    CABasicAnimation * firstAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    firstAnimation.duration = firstDuration;
    firstAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    firstAnimation.fromValue = [NSNumber numberWithFloat:initialValue];
    firstAnimation.toValue = [NSNumber numberWithFloat:middleValue];

    CABasicAnimation * secondAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    secondAnimation.duration = secondDuration;
    secondAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    secondAnimation.fromValue = [NSNumber numberWithFloat:middleValue];
    secondAnimation.toValue = [NSNumber numberWithFloat:1.0f];

    CAAnimationGroup *animationGroup = [CAAnimationGroup new];
    animationGroup.duration = firstDuration + secondDuration;
    animationGroup.animations = @[firstAnimation, secondAnimation];

    [pView.layer addAnimation:animationGroup forKey:nil];
}

有什么想法吗?谢谢!

我会采用不同的技术并使用链式 UIView 块动画,如下所示:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIView *myView          = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 200, 200)];
    myView.backgroundColor  = [UIColor redColor];
    [self initialAnimationFor:myView];
}
- (void)initialAnimationFor:(UIView*)pView {
    pView.transform = CGAffineTransformMakeScale(0.05f, 0.05f);
    if (pView.superview == nil) {
        [self.view addSubview:pView];
    }
    [UIView
     animateWithDuration:0.75f
     animations:^{
         pView.transform = CGAffineTransformMakeScale(1.20f, 1.20f);
     }
     completion:^(BOOL finished) {
         [UIView
          animateWithDuration:0.25f // <-- Your old value of 0.025f makes the animation VERY quick
          animations:^{
              pView.transform = CGAffineTransformIdentity;
          }
          ];
     }
     ];
}

使用此设置,您将获得 "grow to slightly larger than 100% and then 'settle' to 100%" 效果。

这是可行的解决方案吗?