【问题标题】:UIViewPropertyAnimator stops working after presenting another controllerUIViewPropertyAnimator 在呈现另一个控制器后停止工作
【发布时间】:2018-04-09 16:09:49
【问题描述】:

我使用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: 上启动它(保持非活动状态)

【问题讨论】:

    标签: ios objective-c ios11 uiviewpropertyanimator


    【解决方案1】:

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

    iOS 11:您必须将pausesOnCompletion 设置为YES。即使在控制器关闭后,这也会使动画处于活动状态。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多