如何自定义UIScrollview的指标位置?

时间:2016-08-14 04:51:34

标签: swift uitableview uiscrollview horizontalscrollview

我在tableview的单元格中有一个滚动视图。

我试图将scrollview指示器的位置设置到顶部。

enter image description here

我检查了水平指示器。

enter image description here

正如你所看到的......我想把位置设置到顶部。

我的自定义指标:黑色和蓝色指示灯。

默认指标:底部...灰色指示灯..

我也尝试使用方法设置

scrollView.scrollIndicatorInsets = UIEdgeInsets(上:30,左:0,下:0,右:10)

< -It不适合我

class HorizontalCell: UITableViewCell, UIScrollViewDelegate {

    var picArray = [PFFile]()
    var picSectionArray = [UIImageView]()


    @IBOutlet weak var pagingView: UIScrollView!

    override func awakeFromNib() {
        super.awakeFromNib()

        // Initialization code
        let pagingViewHeight = self.pagingView.frame.height
        let pagingViewWidth = self.pagingView.frame.width


        //set section size
        picSectionArray.append(UIImageView(frame: CGRectMake(5, 5 + 3,  pagingViewWidth-10, pagingViewHeight-10 - 3)))
        picSectionArray.append(UIImageView(frame: CGRectMake(pagingViewWidth * 1 + 5, 5 + 3,  pagingViewWidth-10, pagingViewHeight-10 - 3)))
        picSectionArray.append(UIImageView(frame: CGRectMake(pagingViewWidth * 2 + 5, 5 + 3,  pagingViewWidth-10, pagingViewHeight-10 - 3)))


        //add imageview
        self.pagingView.addSubview(picSectionArray[0])
        self.pagingView.addSubview(picSectionArray[1])
        self.pagingView.addSubview(picSectionArray[2])


        self.pagingView.contentSize = CGSizeMake(self.pagingView.frame.width * 3, self.pagingView.frame.height)

//        self.pagingView.scrollIndicatorInsets = self.setScrollIndicatorInsets

        //declaire custom indicator <--How do I implement?
        var scrollIndicator : UIView
        var indicator:UIView

        scrollIndicator = UIView(frame: CGRect(x: 0, y: 0, width: pagingViewWidth * 3, height: 3))
        indicator = UIView(frame: CGRect(x: 0, y: 0, width: pagingViewWidth / 3, height: 3))

        scrollIndicator.backgroundColor = UIColor.blackColor()
        indicator.backgroundColor = UIColor(red: 0.0/255, green: 109/255, blue: 242/255, alpha: 1)

        self.pagingView.addSubview(scrollIndicator)
        self.pagingView.addSubview(indicator)

    }

//It not working
    func scrollViewDidScroll(scrollView: UIScrollView) {
        print("didscroll")
    }

//It now working
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        print("end scrolling")
    }

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。

有两种方法。

<强> 1。使用UIPageController

<强> 2。使用UIView制作自定义指标。

class AdvertiseCell: UITableViewCell, UIScrollViewDelegate {

    var advArray = [PFFile]()
    var advSectionArray = [UIImageView]()




    //declaire Custom Indicator using UIView
    var scrollIndicator = UIView()
    var indicator = UIView()


    @IBOutlet weak var pagingView: UIScrollView!

    @IBOutlet weak var pageController: UIPageControl!

    override func awakeFromNib() {
        super.awakeFromNib()

        // Initialization code
        let pagingViewHeight = self.pagingView.frame.height
        let pagingViewWidth = self.pagingView.frame.width


        //set section size
        advSectionArray.append(UIImageView(frame: CGRectMake(5, 5 ,  pagingViewWidth-10, pagingViewHeight-10)))
        advSectionArray.append(UIImageView(frame: CGRectMake(pagingViewWidth * 1 + 5, 5 ,  pagingViewWidth-10, pagingViewHeight-10)))
        advSectionArray.append(UIImageView(frame: CGRectMake(pagingViewWidth * 2 + 5, 5 ,  pagingViewWidth-10, pagingViewHeight-10)))


        //add imageview
        self.pagingView.addSubview(advSectionArray[0])
        self.pagingView.addSubview(advSectionArray[1])
        self.pagingView.addSubview(advSectionArray[2])


        self.pagingView.contentSize = CGSizeMake(self.pagingView.frame.width * 3, self.pagingView.frame.height)




        self.scrollIndicator = UIView(frame: CGRect(x: 0, y: 0, width: pagingViewWidth, height: 3))
        self.indicator = UIView(frame: CGRect(x: 0, y: 0, width: pagingViewWidth / 3, height: 3))

        self.scrollIndicator.backgroundColor = UIColor.blackColor()
        self.indicator.backgroundColor = UIColor(red: 0.0/255, green: 109/255, blue: 242/255, alpha: 1)

        super.addSubview(scrollIndicator)
        super.addSubview(indicator)


        //It is very important!
        self.pagingView.delegate = self

        self.pageController.currentPage = 0
        self.pageController.numberOfPages = 3


        loadAdvertise()
    }


    func scrollViewDidScroll(sclView: UIScrollView) {
        let page:Int = Int(floor(sclView.contentOffset.x / UIScreen.mainScreen().bounds.size.width))
        self.pageController.currentPage = page


        let pages : CGFloat
        pages = CGFloat(self.pageController.numberOfPages)

        self.indicator.frame = CGRectMake(sclView.contentOffset.x / pages, 0, self.pagingView.frame.width / 3, 3);



    }

查看我的结果

enter image description here

由于