UIScrollView contenOffset和setContentSize?

时间:2013-08-21 07:02:43

标签: ios uiscrollview contentsize contentoffset

我有一个 scrollview ,其中包含许多UIViewUIView对象会动态添加/删除scrollviewcontentSize也会更改setContentSize

我的问题是,如果contentOffset发生变化,其scrollview会自动更改,当我从- (void) removeNameFromTheList:(id)sender{ //[doctorList setScrollEnabled:NO]; UIButton *x = sender; for(UIView *subview in [x.superview.superview subviews]) { if([subview isKindOfClass:[UILabel class]]) { [self redrawTheList:sender:((UILabel*)subview).text]; NSMutableArray * tempo = [[NSMutableArray alloc]init]; tempo = addedList; for(int i=0; i<tempo.count;i++) { if([(NSString*)[tempo objectAtIndex:i] isEqualToString:((UILabel*)subview).text]) { [addedList removeObjectAtIndex:i]; } } NSArray *cells = [tableView visibleCells]; for (DoctorListCell *cell in cells) { UILabel *tempLabel = (UILabel*)subview; if([cell.doctorName.text isEqualToString:tempLabel.text]) { [cell.plusButton setHidden:NO]; } } } } [x.superview.superview removeFromSuperview]; NSLog(@"000:%f",doctorList.contentOffset.y); float temp = doctorList.contentOffset.y; [doctorList setContentSize:CGSizeMake(10, 41+(counterRow*41))]; float temp2 = doctorList.contentOffset.y; [doctorList setContentOffset:CGPointMake(doctorList.contentOffset.x, doctorList.contentOffset.y+(temp2-temp))]; NSLog(@"111: %f",doctorList.contentOffset.y); 删除某些内容时,它不会停留在被删除的位置吗?

如何控制并采取效果?

如果你问我,我会添加代码。

当我删除某些内容时,会使用此方法:

-(UIView*)createDoctorBox : (NSString*)name {


[addedList addObject:name];

NSString *myString = name;

CGSize stringSize = [myString sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];


UIView *doktorKutu = [[UIView alloc]init];

[doctorList addSubview:doktorKutu];

UIView *doktorKutuBas = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 18, 36)];

[doktorKutuBas setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"doktor_isim_kutu_bas"]]];

[doktorKutu addSubview:doktorKutuBas];

UILabel * doktorKutuGovde = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, stringSize.width+3, 36)];

[doktorKutuGovde setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];

[doktorKutuGovde setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"doktor_isim_kutu_1px"]]];

[doktorKutuGovde setUserInteractionEnabled:YES];


[doktorKutuGovde setText:myString];

[doktorKutu addSubview:doktorKutuGovde];



UIView * doktorKutuKic = [[UIView alloc]initWithFrame:CGRectMake(stringSize.width+13, 0, 18, 36)];

[doktorKutuKic setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"doktor_isim_kutu_kic"]]];

[doktorKutu addSubview:doktorKutuKic];

UIImageView *cancelImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cikar"]];

cancelImage.frame = CGRectMake(-5,9, 18, 18);

[doktorKutuKic addSubview:cancelImage];

UIButton *cancel = [[UIButton alloc]initWithFrame:CGRectMake(-5,0, 20, 36)];

//[cancel setBackgroundImage:[UIImage imageNamed:@"cikar"] forState:UIControlStateNormal];

[cancel setUserInteractionEnabled:YES];

[cancel addTarget:self action:@selector(removeNameFromTheList:) forControlEvents:UIControlEventTouchUpInside];

[doktorKutuKic addSubview:cancel];

UITapGestureRecognizer *singlePress =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSinglePress:)];

[doktorKutuGovde addGestureRecognizer:singlePress];

[doktorKutu bringSubviewToFront:cancel];


[doctorCommented addObject:doktorKutu];

[doktorKutuKic bringSubviewToFront:cancel];

[doktorKutu bringSubviewToFront:cancel];

dictRowKey = [NSString stringWithFormat:@"row%d",counterRow];


NSMutableArray *row = [[NSMutableArray alloc]init];

doktorKutu.frame = CGRectMake(5, yAxis, stringSize.width+30, 36);


if([doctorAddition objectForKey:dictRowKey]!=nil)
{
    row = (NSMutableArray*)[doctorAddition objectForKey:dictRowKey];

    if(row.count>0)
    {
        int totalWidth = 5;

        for(int i=0;i<row.count;i++)
        {
                totalWidth = totalWidth + ((UIView*)[row objectAtIndex:i]).frame.size.width+5;
        }

        if(totalWidth+stringSize.width<520)
        {
            doktorKutu.frame = CGRectMake(totalWidth, yAxis, stringSize.width+30, 36);

            [row addObject:doktorKutu];

            [doctorAddition removeObjectForKey:dictRowKey];

            [doctorAddition setObject:row forKey:dictRowKey];
        }
        else
        {
            doktorKutu.frame = CGRectMake(5, yAxis+40, stringSize.width+30, 36);

            yAxis= yAxis + 40.0;
            counterRow++;
            dictRowKey = [NSString stringWithFormat:@"row%d",counterRow];

            row = [[NSMutableArray alloc]init];

            [row addObject:doktorKutu];

            [doctorAddition setObject:row forKey:dictRowKey];



        }

    }

}
else
{
    [row addObject:doktorKutu];

    [doctorAddition setObject:row forKey:dictRowKey];
}    

float temp = doctorList.contentOffset.y;



[doctorList setContentSize:CGSizeMake(10, 41+(counterRow*41))];


float temp2 = doctorList.contentOffset.y;


[doctorList setContentOffset:CGPointMake(doctorList.contentOffset.x, doctorList.contentOffset.y+(temp2-temp))];


if(counterRow>3)
{

    CGPoint bottomOffset = CGPointMake(0, doctorList.contentSize.height - doctorList.bounds.size.height);
    [doctorList setContentOffset:bottomOffset animated:YES];
}



return doktorKutu;

}

当我创建某些内容时,使用此方法:

{{1}}

1 个答案:

答案 0 :(得分:0)

我认为您可以使用 scrollRectToVisible:animated: Methoda来控制scrollview的滚动,您可以以编程方式滚动来解决此问题。

@use

[scrollView scrollRectToVisible:subview.frame animated:NO];