iOS animationWithDuration 链接第一个动画未显示

iOS animationWithDuration chaining first animation isn't showing

我有 2 个动画,一个调用另一个,但实际上只有后者在发生。我不确定问题是什么。我是 iOS 的新手。我正在尝试将其设置为上下动画,但只有向下发生。

此外,如果我将调用从 [self animateUp] 更改为 [self animateDown],则不会出现动画。

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self.containerView addSubview:self.logoView = ({
        UIImageView *imageView = [UIImageView new];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = [UIImage imageNamed:@"thisImage"];
        imageView;
    })];
    [self animateUp];
}

- (void)animateUp
{
    [UIView animateWithDuration:1 animations:^{
        CGRect newFrame = CGRectMake(self.logoView.frame.origin.x, self.logoView.frame.origin.y - 40, self.logoView.frame.size.width, self.logoView.frame.size.height);
        self.logoView.frame = newFrame;
    } completion:^(BOOL finished) {
        [self animateDown];
    }];
}

- (void)animateDown
{
    [UIView animateWithDuration:1 animations:^{
        CGRect newFrame = CGRectMake(self.logoView.frame.origin.x, self.logoView.frame.origin.y + 40, self.logoView.frame.size.width, self.logoView.frame.size.height);
    self.logoView.frame = newFrame;
    } completion:nil ];
}

animateUp 的调用发生在 ImageView 具有初始框架集之前。您可以将其推迟到 viewDidAppearviewDidLayoutSubviews,应该会看到动画正确出现。

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self.containerView addSubview:self.logoView = ({
        UIImageView *imageView = [UIImageView new];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = [UIImage imageNamed:@"thisImage"];
        imageView;
    })];
}

- (void) viewDidLayoutSubviews
{
    [self animateUp];    // at this point the ImageView has its frame set
}

请注意,viewDidAppear 和 viewDidLayoutSubviews 可能会在您的 UIViewController 的生命周期内被多次调用,因此如果您只希望动画出现一次,您将需要适应这一点。