Scrollview应该从我上次离开的地方开始

时间:2016-06-02 05:37:37

标签: ios objective-c uiscrollview

我有一个scrollview(timesScrollView),它被添加为一个视图(dropDownView)的子视图。视图被隐藏,直到按下一个特定的按钮,当按下该按钮时,将出现视图。

(IBAction)how_many_times_btn_click:(id)sender{

if(howMany==false){
    for(UIView *view in dropDownView.subviews)
    {
        [view removeFromSuperview];
    }

howMany=true;
duration=false;
how_many_times_btn.backgroundColor=[UIColor colorWithRed:130/255.0f green:189/255.0f blue:31/255.0f alpha:1.0f];
durationBtn.backgroundColor=[UIColor colorWithRed:62/255.0f green:67/255.0f blue:79/255.0f alpha:1.0f];
startBtn.backgroundColor=[UIColor colorWithRed:62/255.0f green:67/255.0f blue:79/255.0f alpha:1.0f];

dropDownView.hidden=NO;
dropDownView.frame=CGRectMake(0, 0, self.view.frame.size.width,70);
dropDownView.backgroundColor=[UIColor colorWithRed:37/255.0f green:42/255.0f blue:54/255.0f alpha:1.0f];
//dropDownView.backgroundColor=[UIColor whiteColor];


targetLbl=[[UILabel alloc]init];
targetLbl.frame=CGRectMake(0, 30, dropDownView.frame.size.width,30);
targetLbl.text=@"TARGET";
targetLbl.textColor=[UIColor whiteColor];
targetLbl.font=[UIFont boldSystemFontOfSize:22];
targetLbl.textAlignment=NSTextAlignmentCenter;


how_many_Lbl=[[UILabel alloc]init];
how_many_Lbl.frame=CGRectMake(0, targetLbl.frame.origin.y+targetLbl.frame.size.height, dropDownView.frame.size.width, 20);
how_many_Lbl.textAlignment=NSTextAlignmentCenter;
how_many_Lbl.text=@"HOW MANY TIMES WILL YOU DO IT?";
how_many_Lbl.textColor=[UIColor colorWithRed:65/255.0f green:71/255.0f blue:80/255.0f alpha:1.0f];
    how_many_Lbl.font=[UIFont systemFontOfSize:10.0f];


hideViewBtn=[UIButton buttonWithType:UIButtonTypeCustom];
hideViewBtn.frame=CGRectMake(dropDownView.frame.size.width-30,20,20,20);
[hideViewBtn setImage:[UIImage imageNamed:@"Close Icon [ x ]"] forState:UIControlStateNormal];
[hideViewBtn addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
    //hideViewBtn.backgroundColor=[UIColor whiteColor];

self.timesScroll=[[LTInfiniteScrollView alloc] initWithFrame:CGRectMake(0, how_many_Lbl.frame.origin.y+how_many_Lbl.frame.size.height+16, dropDownView.frame.size.width, 102)];
//self.timesScroll.backgroundColor=[UIColor whiteColor];
self.timesScroll.verticalScroll=NO;
self.timesScroll.dataSource=self;
self.timesScroll.maxScrollDistance=5;
self.timesScroll.contentInset=UIEdgeInsetsMake(0, self.timesScroll.frame.size.width/2-31, 0,self.timesScroll.frame.size.width/2-31 );
self.timesScroll.userInteractionEnabled=YES;
self.timesScroll.exclusiveTouch=YES;
dropDownView.frame=CGRectMake(0, 0, self.view.frame.size.width,_timesScroll.frame.origin.y+_timesScroll.frame.size.height+20);

    [self viewWillAppear:YES];

    [dropDownView addSubview:targetLbl];
    [dropDownView addSubview:how_many_Lbl];
    [dropDownView addSubview:hideViewBtn];
    [dropDownView addSubview:_timesScroll];

}
else
{
    [self hideView];
}

}

上面的方法是我用来创建视图的方法。 现在我的问题是,当再次按下该特定按钮(how_many_times_btn)时,首先删除所有视图,然后添加,因为您可以看到并且scrollview从初始位置开始,但我希望它显示从我上次单击how_many_times_btn时离开它的位置。 希望你能理解我想说的话......如果不是,我很乐意详细说明。

3 个答案:

答案 0 :(得分:0)

你可以通过委托方法获得最后的位置

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    nslog(@"%f %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}

并存储x和y值并设置

scrollView.contentOffset = CGPointMake(x,y);

答案 1 :(得分:0)

您可以将contentOffset保存到CGPoint的一个变量中。稍后使用此变量的值来滚动UIScrollview

下面的代码行:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    contentOffset = scrollView.contentOffset;
}

按下按钮时,在代码行下面写下:

 self.timesScroll .contentOffset =  contentOffset;

答案 2 :(得分:0)

我正在使用LTInfiniteScrollview,其中有一个方法'reloadDataWithInitialIndex'

-(void)reloadDataWithInitialIndex:(NSInteger)initialIndex

{
for (UIView *view in self.scrollView.subviews) {
    [view removeFromSuperview];
}
self.views = [NSMutableDictionary dictionary];

self.visibleViewCount = [self.dataSource numberOfVisibleViews];
self.totalViewCount = [self.dataSource numberOfViews];

[self updateSize];
_currentIndex = initialIndex;
self.scrollView.contentOffset = [self contentOffsetForIndex:_currentIndex];
[self reArrangeViews];
[self updateProgress];}

此方法在'viewWillAppear'

中调用
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.viewSize = CGRectGetWidth(self.view.frame) / Number_of_visibleViews;
self.timesScroll.delegate=self;
[self.timesScroll reloadDataWithInitialIndex:howIndex];}

我刚刚在这里传递了以前的索引值。