@interface ViewController ()<UIScrollViewDelegate>
{
UIScrollView *backScrollView;
UIImageView *scrollIndexerImageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, 200, 300)];
backScrollView.backgroundColor =
[UIColor greenColor];
backScrollView.contentSize = CGSizeMake(200, 900);
backScrollView.showsHorizontalScrollIndicator = NO;
backScrollView.showsVerticalScrollIndicator = NO;
backScrollView.delegate = self;
[self.view addSubview:backScrollView];
scrollIndexerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(backScrollView.frame.size.width-8, 0, 8,30)];
scrollIndexerImageView.image =
[UIImage imageNamed:@"teacherlist_select_scrollbar"];
[backScrollView addSubview:scrollIndexerImageView];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
float p = 0;
p = scrollView.contentOffset.y/(scrollView.contentSize.height -
scrollView.frame.size.height+(scrollIndexerImageView.frame.size.height-scrollIndexerImageView.frame.size.height*scrollView.frame.size.height/scrollView.contentSize.height));
scrollIndexerImageView.frame = CGRectMake(backScrollView.frame.size.width-8,p*scrollView.contentSize.height,8,30);
NSLog(@"y:%f",scrollView.contentOffset.y);
NSLog(@"--:%f",scrollIndexerImageView.frame.origin.y);
}
原文地址:http://www.wahenzan.com/a/mdev/ios/2014/1215/350.html