ACTabScrollView icon indicating copy to clipboard operation
ACTabScrollView copied to clipboard

function currentPageIndex possible crash

Open Minitour opened this issue 9 years ago • 2 comments

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.

Minitour avatar Jul 04 '16 14:07 Minitour

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

azurechen avatar Jul 23 '16 12:07 azurechen

is it fixed?

choiks14 avatar Dec 26 '16 04:12 choiks14