gpt4 book ai didi

ios - UIViewPropertyAnimator 在呈现另一个 Controller 后停止工作

转载 作者:行者123 更新时间:2023-11-29 00:00:08 24 4
gpt4 key购买 nike

我使用 UIViewPropertyAnimator 来缩小我的 View Controller 的 header :

- (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;
}

滚动时,我设置了片段:

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

这很好用。但是在我从这个 Controller 中呈现另一个 UIViewController 并将其关闭后,动画师进入非事件状态并停止响应分数设置。我怎样才能重新启动它?

我已经尝试过的:

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

最佳答案

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

iOS 11:您必须将 pausesOnCompletion 设置为 YES。即使在 Controller 被关闭后,这也会使动画处于事件状态。

对于早期版本,我必须离开 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];
}

关于ios - UIViewPropertyAnimator 在呈现另一个 Controller 后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49737220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com