如何使用scrollRectToVisible:动画?

时间:2016-01-07 18:15:35

标签: ios uiscrollview

我有tableView,在其中一行中我有一个水平scrollView和一些图像(图像在行的不同视图中),在水平scrollView中我添加了几个按钮(10个按钮)。当用户点击scrollView中的任何按钮时,我会调用reloadData。因为我调用reloadData所以水平scrollView再次使用新图像数据重新加载。现在我希望如果用户滚动结束并单击scrollView中的最后一个按钮,用户应该能够看到所选按钮而不是第一个按钮。我不知道如何使用scrollRectToVisible:animated来实现这一目标。

这是我的scrollView代码并在其上添加按钮。

-(void)addColorFiltersOnView:(UIView *)colorView{       
    if (_colorFilterView) {
        [_colorFilterView removeFromSuperview];
    }

    self.colorFilterView = [[UIScrollView alloc] initWithFrame:colorView.bounds];
    [_colorFilterView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
    [_colorFilterView setBackgroundColor:samergb(238)];
    [_colorFilterView setShowsHorizontalScrollIndicator:NO];

    int addedCount = 0;
    int i=0;

    CGFloat x = 10, y = 5;

    if ([_colorFilters count] < 1) {
        return;
    }

    for (i=0; i<[_colorFilters count] - 1; i++) {

        id item = [_colorFilters objectAtIndex:i];

        if ([item isKindOfClass:[NSString class]]){

            if ([self.prodVIPDictionary.PDcolor isEqualToString:(NSString*)item] && i != 0) {
                continue;
            }

            NSInteger itemsCount = [[_colorFilters objectAtIndex:i+1] integerValue];

            if (itemsCount  <= 0) {
                break;
            }

            TTTAttributedLabel *itemLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
            [itemLabel setNumberOfLines:1];
            [itemLabel setTextAlignment:NSTextAlignmentCenter];
            [itemLabel setFont:[UIFont systemFontOfSize:14]];

            if ([_selectedColor isEqualToString:(NSString*)item])
                [itemLabel setTextColor:[UIColor whiteColor]];

            [itemLabel setText:[(NSString*)item capitalizedString]];
            [itemLabel setBackgroundColor:samergb(255)];
            itemLabel.layer.borderWidth = 1;
            itemLabel.layer.masksToBounds = YES;
            itemLabel.layer.cornerRadius = 10;
            itemLabel.layer.borderColor = samergb(210).CGColor;
            [itemLabel sizeToFit];
            [_colorFilterView addSubview:itemLabel];

            CGRect itemFrame = itemLabel.frame;
            itemFrame.size.width += 20;
            [itemLabel setFrame:itemFrame];

            UIButton *optionBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, itemLabel.frame.size.width + 6, 26)];
            [optionBtn setTag:[_colorFilters indexOfObject:item]];
            [optionBtn addTarget:self action:@selector(optionColorClicked:) forControlEvents:UIControlEventTouchUpInside];

            [_colorFilterView addSubview:optionBtn];

            [itemLabel setFrame:optionBtn.frame];

            if ([_selectedColor isEqualToString:(NSString*)item]) {
                [optionBtn setEnabled:NO];
                [itemLabel setBackgroundColor:[LRUtils lrGreenColor]];
            }

            addedCount ++;

            x += optionBtn.frame.size.width+5;

            if (addedCount >= 20) {
                break;
            }

        }else{
            continue;
        }
    }

    [_colorFilterView setContentSize:CGSizeMake((x + 10),colorView.frame.size.height)];
    [colorView addSubview:_colorFilterView];
}

1 个答案:

答案 0 :(得分:0)

您可以在表视图上调用此方法,而不是调用reloadData

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

您需要创建一个NSArray,其中包含表视图中除之外的所有IndexPath,其中包含滚动视图和按钮的行。这样,滚动视图就不会被重置。