自定义UIScrollView 滚动条(适合大部分继承自UIScrollView的控件,如UITextView,UITableView等)

转载自:http://blog.csdn.net/liubo0_0/article/details/7067460


滚动条只是显示作用,不能点击拖动。
当然可以根据自定义样式。

想法很简单,就是在UIScrollView里面先增加一个竖条,在增加一个图标。

通过移动UIScrollView里面的坐标,来实现图标移动的效果。


    UIScrollView *leftScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, 200, 630)];
    leftScroll.delegate = self;
    leftScroll.showsVerticalScrollIndicator = NO;
    leftScroll.bounces = NO;
    [self.view addSubview:leftScroll];

    // 添加 竖条 
    NSString *pathS = [[NSBundle mainBundle] pathForResource:@"ss" ofType:@"png"];
    UIImage *imageS = [[UIImage alloc] initWithContentsOfFile:pathS];
    UIImageView *imageSView = [[UIImageView alloc] initWithImage:imageS];
    imageSView.frame = CGRectMake(190,0,2,200*[arr count]); //第一处
    [leftScroll addSubview:imageSView];
    
    // 添加 图标
    pathS = [[NSBundle mainBundle] pathForResource:@"gg" ofType:@"png"];
    imageS = [[UIImage alloc] initWithContentsOfFile:pathS];
    imageGView = [[UIImageView alloc] initWithImage:imageS];
    imageGView.frame = CGRectMake(186,10,10,27);
    [leftScroll addSubview:imageGView];


// 当移动调用此方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{ //第二处
    float p = 0;
    p = scrollView.contentOffset.y/(scrollView.contentSize.height - scrollView.frame.size.height);
    imageGView.frame = CGRectMake(186,p*scrollView.contentSize.height,10, 27);
}


//实际使用中对两处做了修改,因为我是在自定义UITextView时引用的代码

第一处:

  float h =  (self.label_alert_info.frame.size.height * self.label_alert_info.frame.size.height)/self.label_alert_info.contentSize.height;

    imageGView.frame = CGRectMake(256,0,5,h);

第二处:

 if (([scrollView isKindOfClass:[UITextView class]])) {

        float h = 0.0;        

        float p = 0.0;

        h =  (scrollView.frame.size.height * scrollView.frame.size.height)/scrollView.contentSize.height;

        p = (scrollView.contentOffset.y + scrollView.frame.size.height)/scrollView.contentSize.height*scrollView.frame.size.height - h;

        imageGView.frame = CGRectMake(256,p + scrollView.contentOffset.y,5,h);

    }


你可能感兴趣的:(自定义UIScrollView 滚动条(适合大部分继承自UIScrollView的控件,如UITextView,UITableView等))