0

我在 stackoverflow 上阅读了一百万篇关于 nstimers、runloops 和主线程的帖子,但它们似乎没有我正在寻找的相同问题或答案。我的情况:

  1. 我正在尝试为一些东西制作动画,同时有一些“正在加载”。动画由几个使用 contentOffset 来回移动的滚动视图组成。我使用 NSTimer 来制作这个动画。
  2. 第 1 点的“正在加载”实际上不是一些简单的加载,而是在用户当前不可见的其他一些 UIView 上进行绘制。

所以总而言之,我试图在 UIViews 上同时绘制一些滚动视图的动画。如果它们没有以动画不流畅的方式发生冲突,这甚至可能吗?如果我使用 activityIndi​​catorView 绝对有可能,纺车永远不会结结巴巴,它总是在平稳旋转。我试过使用runloops,但我想不通。到目前为止,这是我的一些代码:

-(void)startLoading
{
    [self performSelectorInBackground:@selector(showSpinningCells)];
    [self drawOnViews];
}

-(void)drawOnViews
{
    //HERE A LOT OF VIEWS ARE BEING UPDATED AND CALLED SETNEEDSDISPLAY
}

-(void)spinCells
{
    //HERE I USE PERFORMSELECTORONMAINTHREAD TO SET THE
    //CONTENTOFFSET OF THE SCROLLVIEWS
}

-(void)showSpinningCells
{    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    spinLoop = [NSRunLoop currentRunLoop];
    spinTimer = nil;
    self.userInteractionEnabled = FALSE;
    spinTimer = [[NSTimer scheduledTimerWithTimeInterval:0.2 
                            target:self 
                          selector:@selector(spinCells) 
                          userInfo:nil 
                           repeats:YES] retain];    
    [spinLoop run]; 
    [pool release];
}

在我进一步尝试之前,请告诉我这是否可能,如果可以,怎么做?目前我从未见过任何旋转。如果我之后不做任何事情,我确实会看到旋转,但是一旦我在 UIViews 上调用绘图,旋转就会完全停止。

4

2 回答 2

0

你可以试试这个

-(void)showSpinningCells
{    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    spinTimer = nil;
    self.userInteractionEnabled = FALSE;
    spinTimer = [[NSTimer timerWithTimeInterval:0.2
                                        target:self
                                      selector:@selector(spinCells) 
                                      userInfo:nil
                                        repeats:YES] retain];
    [[NSRunLoop currentRunLoop] addTimer:spinTimer forMode:NSRunLoopCommonModes];
    [pool release];
}

添加timercurrentRunLoop_NSRunLoopCommonModes

于 2012-05-24T11:08:01.283 回答
0

UI 动画只能在您的 drawOnViews 方法退出后发生,以便 UI 运行循环可以获得在主线程中执行动画所需的时间。一种解决方案是将您的 drawOnViews 方法分割成非常短的异步段,每个段快速退出,以便 UI 动画可以在每个段之间继续。

于 2012-05-25T01:36:28.520 回答