自定义滚动指示器

时间:2012-08-31 14:11:39

标签: iphone objective-c ios

有没有办法在UIScrollView中自定义滚动指示符(垂直或水平)。例如,在scrollview中更改它的颜色或位置。

1 个答案:

答案 0 :(得分:5)

首先,您必须在Interface Builder中取消选中showVerticalIndicator和showHorizo​​ntallIndicator。

然后在viewDidLoad方法中添加:

[super viewDidLoad];    
self.scrollViewBarImgView = [[UIImageView alloc] init];
UIImage *imgBar = [UIImage imageNamed:@"scroller.png"];
[self.scrollViewBarImgView setImage:imgBar];
CGRect frame = scrollViewBarImgView.frame;
frame.size.width = 8;
frame.size.height = 60;
frame.origin.x = 312;
frame.origin.y = 0;
[self.scrollViewBarImgView setFrame:frame];

[self.tableView addSubview:scrollViewBarImgView];

然后在- (void)scrollViewDidScroll方法中:

CGPoint offset = scrollView.contentOffset;
CGRect frame = self.scrollViewBarImgView.frame;

float fact = (cellHeight*numberOfRow+ indicatorBarHeight) / tableViewHeight;

frame.origin.y = offset.y + (offset.y/fact);

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
[self.scrollViewBarImgView setFrame:frame];
[UIView commitAnimations];

这对我有用。