【问题标题】:UIScrollView setContentOffset: animated: not workingUIScrollView setContentOffset:动画:不工作
【发布时间】:2011-05-04 10:48:59
【问题描述】:

我有两个无限 UIScrollViews 的问题,它们没有正确更改它们的偏移量。

第一个 UIScrollView 中的一个项目对应于第二个中的另一个。然后,我想将点击的项目放在第二位,并为两个 UIScrollView 中的更改设置动画。为了正确完成这项工作,我使用以下两种方法:

当项目被点击时:

- (void)click:(id)sender {
    NSInteger index = [[self cells] indexOfObject:sender];
    if (index == 1)
    {
        CategoriesViewController *viewController = [[CategoriesViewController alloc] initWithNibName:@"CategoriesView" bundle:nil];

        [[viewController tit] setText:NSLocalizedString([[self categories] objectAtIndex:1], nil)];

        [[self navigationController] pushViewController:viewController animated:YES];
    }
    else
    {
        for (NSInteger i = 0; i < index - 1; i++)
        {
            NSObject *object = [[self categories] objectAtIndex:0];
            [[self categories] removeObjectAtIndex:0];
            [[self categories] addObject:object];
        }

        for (NSInteger i = 0; i < index - 1; i++)
        {
            CollectionCellViewController *cell = [[self cells] objectAtIndex:0];
            [[self cells] removeObjectAtIndex:0];
            [[self cells] addObject:cell];
        }

        for (NSInteger i = 0; i < index - 1; i++)
        {
            UIImageView *image = [[self images] objectAtIndex:0];
            [[self images] removeObjectAtIndex:0];
            [[self images] addObject:image];
        }

        [self updateViewsOffsets];
    }
}

- (void)updateViewsOffsets {
    NSInteger y = 0;
    for (NSInteger i = 0; i < [[self cells] count]; i++)
    {
        CollectionCellViewController *cell = [[self cells] objectAtIndex:i];

        [[cell view] setTag:i];
        if (i == 1)
        {
            [cell setSelected:[[self categories] objectAtIndex:i]];
        }
        else
        {
            [cell setDeselected:[[self categories] objectAtIndex:i]];
        }

        CGRect rect = [[cell view] frame];
        rect.origin.x = 0.0f;
        rect.origin.y = y;
        [[cell view] setFrame:rect];
        y += [[self table] frame].size.height / 2.0f;
    }

    y = 0;
    for (NSInteger i = 0; i < [[self images] count]; i++)
    {
        UIImageView *image = [[self images] objectAtIndex:i];
        CGRect rect = [image frame];
        rect.origin.x = 0.0f;
        rect.origin.y = y;
        [image setFrame:rect];
        y += [[self gallery] frame].size.height;
    }

    [[self table] setContentOffset:CGPointMake(0.0f, [[self table] frame].size.height / 2.0f) animated:YES];
    [[self gallery] setContentOffset:CGPointMake(0.0f, [[self table] contentOffset].y * 8.0f) animated:YES];
}

进行了更改,但没有动画,它与YES或NO具有相同的行为,它只调用scrollViewDidScroll:一次。但是,我在其他方法中使用 setContentOffset: animated: 在它与 UIScrollView 一起正常工作的其他方法中。

任何想法,请。非常感谢您。

【问题讨论】:

  • iOS 4.2+?它有严重的滚动错误...
  • @jv42,完全正确。我也尝试过动画块,但我想模拟滚动并且没有这样的过渡。您还有其他可能的解决方案吗?
  • 我记得一些可怕的事情,比如将 UIScrollView 的大小调整为 0,然后设置内容,然后调整为实际大小。

标签: iphone animation uiscrollview offset


【解决方案1】:

我刚遇到这个问题。我通过将调用调度到主线程来修复它。试试这个:

dispatch_async(dispatch_get_main_queue(), ^{
    [[self table] setContentOffset:CGPointMake(0.0f, [[self table] frame].size.height / 2.0f) animated:YES];
    [[self gallery] setContentOffset:CGPointMake(0.0f, [[self table] contentOffset].y * 8.0f) animated:YES];
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多