0

我在滚动视图中的内容上制作了动画,但内存有问题。所以我有 UITabBarController,在 3 个选项卡中我有一个自定义 UIView,它有一个 UIScrollView。我用它来动画水平内容滚动:

- (void)beginAnimation
{
if (isAnimating) {
    return;
}

[scrollView setContentOffset:[self startOffset]];

isAnimating = YES;

NSTimeInterval animationDuration = (scrollView.contentSize.width / self.tickerSpeed);

[UIView animateWithDuration:animationDuration
                      delay:0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     CGPoint finalPoint = CGPointZero;

                     if (self.scrollingDirection == BBScrollingDirectionFromRightToLeft) {
                         finalPoint = CGPointMake(scrollView.contentSize.width, 0);
                     } else if (self.scrollingDirection == BBScrollingDirectionFromLeftToRight) {
                         finalPoint = CGPointMake(-scrollView.contentSize.width + self.frame.size.width, 0);
                     }

                     scrollView.contentOffset = finalPoint;
                 } completion:^(BOOL finished) {
                         isAnimating = NO;

                         [self beginAnimation];
                 }];
}

当我启动应用程序并位于第一个选项卡时,一切正常,但是当我切换到另一个选项卡时,仪器分配中的总字节数开始快速增长,实时字节数几乎相同。有人可以解释一下发生了什么吗?

4

1 回答 1

1

我认为你正在创建一个无限循环

completion:^(BOOL finished) {
                         isAnimating = NO;

                         [self beginAnimation];
                 }];

为什么要通过完成块递归调用开始动画?这将是我对您的内存问题的猜测,块像其他 obj-c 对象一样保存到内存中,并且它们占用空间。

编辑:

我建议您像这样修改对 smt 的动画调用,并再次检查您是否有内存问题:

[UIView animateWithDuration:animationDuration
                      delay:0
                    //I added autoreverse option also bc it seems like a good fit for your purpose
                    options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                 animations:^{
                     [UIView setAnimationRepeatCount:10.0]; //This a class method, set repeat count to a high value, use predefined constants (ie HUGE_VALF) if it works for you 
                     ...
                 }
];
于 2013-02-24T22:22:42.557 回答