azurechen / ACTabScrollView

A fancy Menu and Pager UI extends UIScrollView with elegant, smooth and synchronized scrolling tabs.
MIT License
115 stars 35 forks source link

function currentPageIndex possible crash #2

Open Minitour opened 8 years ago

Minitour commented 8 years ago

The function can throw an error if the width is equal to 0 - which may be possible in some cases.

So I replaced this:

private func currentPageIndex() -> Int {
        let width = self.frame.width
        var currentPageIndex = Int((contentSectionScrollView.contentOffset.x + (0.5 * width)) / width)
        if (currentPageIndex < 0) {
            currentPageIndex = 0
        } else if (currentPageIndex >= self.numberOfPages) {
            currentPageIndex = self.numberOfPages - 1
        }
        return currentPageIndex
    }

With this:

private func currentPageIndex() -> Int {
        let width = self.frame.width

        var currentPageIndex = 0
        if width != 0 {
           currentPageIndex =   Int(( contentSectionScrollView.contentOffset.x + (0.5 * width)) / width)
        } 

        if (currentPageIndex < 0) {
            currentPageIndex = 0
        } else if (currentPageIndex >= self.numberOfPages) {
            currentPageIndex = self.numberOfPages - 1
        }
        return currentPageIndex
    }

A simple validation to avoid division by zero.

azurechen commented 8 years ago

Thank you! I will fix it at next version. Or you can send a PR of your patch, I will very appreciate

choiks14 commented 7 years ago

is it fixed?