iphone - UIScrollView setContentOffset : animated: not working

标签 iphone animation uiscrollview offset

我遇到两个无限 UIScrollView 的问题,它们无法正确更改其偏移量。

第一个 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];
}

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

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

最佳答案

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

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

关于iphone - UIScrollView setContentOffset : animated: not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5882232/

相关文章:

ios - 创建 UICollectionViewCell 时如何在发件人中添加标签值?

android - iphone 和 android 的 Css 区别?

android - MotionLayout 忽略填充

css - 单击鼠标隐藏/显示 css 动画

ios - Scrollview 在 Xcode 8 中无法自动调整大小

ios - 这个属性声明是什么意思?

iphone - 生成当月的随机日期

c# - 动画 StackPanel 高度代码隐藏

objective-c - UIScrollView 内的水平平移手势

ios - 缩放后文字变得模糊