然而,作为相对新手的Instruments内存分配者,我无法准确地解释52%的分配是在哪里进行的,如下面屏幕截图所示:
显然,这与核心动画有关,但是具体是什么我很难确定,因此我认为那里一定有聪明人可能知道答案。
导航:
我的应用程序在视图控制器之间移动时使用自定义转场,其中涉及大量动画。以下是一个示例:
@interface AreaToKeyFiguresSegue : UIStoryboardSegue
@end
...
@implementation AreaToKeyFiguresSegue
- (void)perform
{
[self sourceControllerOut];
}
- (void)sourceControllerOut
{
AreaChooserViewController *sourceViewController = (AreaChooserViewController *) [self sourceViewController];
KeyFigureViewController *destinationController = (KeyFigureViewController *) [self destinationViewController];
double ratio = 22.0/sourceViewController.titleLabel.font.pointSize;
sourceViewController.titleLabel.adjustsFontSizeToFitWidth = YES;
[UIView animateWithDuration:TRANSITION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
// Animate areaChooser
sourceViewController.areaChooserScrollView.alpha = 0;
sourceViewController.areaScrollViewVerticalSpaceConstraint.constant = -300;
sourceViewController.backButtonVerticalConstraint.constant = 20;
sourceViewController.backButton.transform = CGAffineTransformScale(sourceViewController.backButton.transform, ratio, ratio);
sourceViewController.backButton.titleLabel.textColor = [UIColor redKombitColor];
sourceViewController.backArrowPlaceholderVerticalConstraint.constant = 14;
sourceViewController.backArrowPlaceholder.alpha = 1;
sourceViewController.areaLabelVerticalConstraint.constant = 50;
sourceViewController.areaLabel.alpha = 1;
[sourceViewController.view layoutIfNeeded];
} completion:^(BOOL finished) {
[destinationController view]; // Make sure destionation view is initialized before animating it
[sourceViewController.navigationController pushViewController:destinationController animated:NO]; // Push new viewController without animating it
[self destinationControllerIn]; // Now animate destination controller
}];
}
- (void)destinationControllerIn
{
AreaChooserViewController *sourceViewController = (AreaChooserViewController *) [self sourceViewController];
KeyFigureViewController *destinationController = (KeyFigureViewController *) [self destinationViewController];
destinationController.keyFigureTableViewVerticalConstraint.constant = 600;
destinationController.keyFigureTableView.alpha = 0.0;
destinationController.allFavoritesSegmentedControl.alpha = 0.0;
[destinationController.view layoutIfNeeded];
[sourceViewController.segueProgress setHidden:YES];
}
@end
每当一个视图控制器需要被弹出时,我只需执行相反的操作:
- (IBAction)goBack:(id)sender
{
[UIView animateWithDuration:TRANSITION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.keyFigureTableView setAlpha:0];
self.keyFigureTableViewVerticalConstraint.constant = 700;
[self.allFavoritesSegmentedControl setAlpha:0];
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[self.navigationController popViewControllerAnimated:NO]; // Pop viewController without animating it
}];
}
编辑:
大部分内存分配发生在推送视图控制器时,即使它之前已经显示过。也就是说,从
A -> B -> C
B <- C
B -> C
其中“->” = 推送,“<-” = 弹出,每个“->”都会分配更多的内存,“<-”不会释放任何内存。
更多细节
根据Instruments的结果,我没有僵尸对象和内存泄漏,并且静态分析也没有发现问题。我的应用程序只是不断地分配内存,直到最终崩溃。
我大约有70%的内存分配发生在以下调用堆栈中,与我的代码无关(倒置的调用树):
layoutIfNeeded
,基本上就没有动画了。 - ZappeldrawRect
。我只是对所有东西使用自动布局。 - Zappel