2012-08-31 129 views
3

有沒有辦法在UIScrollView中自定義滾動指標(垂直或水平)。例如,在滾動視圖中更改它的顏色或位置。自定義滾動指標

回答

4

首先,您必須在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]; 

這是爲我工作。