使用 UIProgressView 指定 UIScrollView 的滚动

Specified the scroll of a UIScrollView with an UIProgressView

我想在导航栏下添加一个进度条,用于指示 UIScrollView 滚动的进度,我使用(无效):我做

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.progressView.progress = scrollView.contentOffset.y;
}

现在检查我创建了一个演示:

       #import "ViewController.h"

    @interface ViewController ()<UIScrollViewDelegate>{
        UIScrollView *scroll;
        UIProgressView *indicater;
        }
    @end

    @implementation ViewController



    - (void)viewDidLoad {
        indicater=[[UIProgressView alloc]initWithFrame:CGRectMake(0, 65, 320,10)];
        indicater.backgroundColor=[UIColor redColor];
        indicater.progress = 0.0;
        indicater.hidden=false;


        [self.view addSubview:indicater];

        scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 80,300, 200)];
        scroll.backgroundColor=[UIColor greenColor];
        scroll.userInteractionEnabled=YES;
        scroll.delegate=self;
        scroll.contentSize = CGSizeMake(300 ,5000);

        [self.view addSubview:scroll];



    }
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{

        CGPoint offset = scroll.contentOffset;
        CGRect bounds = scroll.bounds;
        CGSize size = scroll.contentSize;
        UIEdgeInsets inset = scroll.contentInset;
        float y = offset.y + bounds.size.height - inset.bottom;
        float h = size.height;
        NSLog(@"%f",y);
        NSLog(@"%f",h);
        NSNumber *num=[NSNumber numberWithFloat:y];
        NSNumber *num1=[NSNumber numberWithFloat:h];



    [self UpdateProgressbar:num TotalscrollLenrth:num1];

}

-(void)UpdateProgressbar:(NSNumber*)currentscrollLenrth TotalscrollLenrth:(NSNumber*)n
{

    NSString *totalLength = [NSString stringWithFormat:@"%@", n];
    NSLog(@"%@", totalLength);

    NSString *currentScrool = [NSString stringWithFormat:@"%@", currentscrollLenrth];
    NSLog(@"%@", currentScrool);
    if (currentscrollLenrth <= n) {
        [indicater setProgress:([currentScrool intValue]/[totalLength floatValue])];
    }
    else {
                  // do somithig

    }
}