【问题标题】:Animation content on UIScrollView memory allocationUIScrollView 内存分配上的动画内容
【发布时间】:2013-02-24 22:16:13
【问题描述】:

我在滚动视图中的内容上制作了动画,但我遇到了内存问题。所以我有 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];
                 }];
}

当我启动应用程序并位于第一个选项卡上时,一切正常,但当我切换到另一个选项卡时,仪器分配中的总字节数开始快速增长,实时字节数几乎相同。 谁能解释一下这是怎么回事?

【问题讨论】:

    标签: ios animation uiscrollview uiviewanimation


    【解决方案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 
                         ...
                     }
    ];
    

    【讨论】:

    • 那么我应该怎么做才能一直为我的内容制作动画,比如在一些股票视图中?
    • 但是如果我切换到其他选项卡它不起作用,那么动画也会停止,如果我回到第一个选项卡它就不再动画了。
    • 所以你的回答现在帮助我解决了这个问题。我使用 UIViewAnimationOptionRepeat 重复动画并删除 [self beginAnimation];从完成块(它使内存增长)开始动画从 viewDidLoad 移动到 viewWillAppear 方法。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    相关资源
    最近更新 更多