UIScrollView setContentOffset:动画:不起作用

发布于 2024-11-05 04:12:32 字数 2770 浏览 2 评论 0原文

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

第一个 UIScrollView 中的项目与第二个 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];
}

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

有什么想法吗?预先非常感谢您。

I have a problem with two infinite UIScrollViews which do not change their offsets properly.

An item in the first UIScrollView corresponds with another one in the second. Then, I want to put the clicked item in the second place and animate the change in both UIScrollView. To get this done properly, I use this two methods:

When item is clicked:

- (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];
}

The change is made, but without animation, it have the same behaviour with YES or NO, it only calls scrollViewDidScroll: once. However, I use setContentOffset: animated: in other methods where it works properly with both UIScrollView.

Any idea, please. Thank you very much in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2024-11-12 04:12:32

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

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];
});

I just had this issue. I fixed it by dispatching the call to the main thread. Try this:

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];
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文