2

UIViewPropertyAnimator用于缩小视图控制器的标题:

- (UIViewPropertyAnimator *)headerAnimator {
    if (!_headerAnimator) {
        _headerAnimator = [[UIViewPropertyAnimator alloc] initWithDuration:2.f curve:UIViewAnimationCurveLinear animations:^{
            self.profileImageBorderWidthConstraint.constant = 58.f;
            self.profileImageBorderHeightConstraint.constant = 58.f;
            self.profileImageBorder.layer.cornerRadius = 29.f;

            self.profileImageView.layer.cornerRadius = 25.f;

            self.nameLabelTopConstraint.constant = 16.f;

            [self.headerView layoutIfNeeded];
        }];
    }

    return _headerAnimator;
}

在滚动时,我设置了fragment

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat fraction = (scrollView.contentOffset.y / (HeaderHeight-CollapsedHeaderHeight));
    self.headerAnimator.fractionComplete = fraction;
}

这工作正常。但是在我从这个控制器中展示另一个UIViewController并关闭它之后,动画师进入非活动状态并停止响应分数设置。我怎样才能重新启动它?

我已经尝试过的:

  • 将我的 animator 属性设置为nil, 以便下次重新创建它(崩溃)
  • 暂停它-viewWillAppear:变得活跃(变得活跃,但不动画)
  • 启动它-viewWillAppear:(保持非活动状态)
4

1 回答 1

3

到目前为止,我找到了适用于 iOS 11+ 的完美解决方案和适用于早期操作系统的解决方法。

iOS 11:您必须设置pausesOnCompletionYES. 即使在控制器被解除后,这也会使动画处于活动状态。

对于早期版本,我必须离开UIViewPropertyAnimator并创建一个自己的函数,在该函数中动画分数将更改并手动插入值:

- (void)animateHeaderToScrollPoint {
    // Header
    CGFloat fraction = self.tableView.contentOffset.y / (HeaderHeight-CollapsedHeaderHeight);
    fraction = fminf(fraction, 1.f);
    fraction = fmaxf(fraction, 0.f);
    fraction = 1.f - fraction;

    self.profileImageBorderWidthConstraint.constant = 58.f + (86.f - 58.f) * fraction;
    self.profileImageBorderHeightConstraint.constant = 58.f + (86.f - 58.f) * fraction;
    self.profileImageBorder.layer.cornerRadius = 29.f + (43.f - 29.f) * fraction;

    self.profileImageView.layer.cornerRadius = 25.f + (39.f - 25.f) * fraction;

    self.nameLabelTopConstraint.constant = 16.f + (31.f - 16.f) * fraction;

    [self.headerView layoutIfNeeded];
}
于 2018-04-10T15:43:08.710 回答