Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmatic way to go to next/previous month #47

Closed
BaptisteSansierra opened this issue Jun 13, 2016 · 3 comments
Closed

Programmatic way to go to next/previous month #47

BaptisteSansierra opened this issue Jun 13, 2016 · 3 comments
Labels

Comments

@BaptisteSansierra
Copy link
Contributor

BaptisteSansierra commented Jun 13, 2016

I'v added arrow buttons to go to next/previous month (image posted on 18 june), but it seems there's no way to do this programmatically at the moment ?

I did it adding these funcs in JTAppleCalendarView.swift

`

public func scrollToNextSection(completion: () -> Void) {
    let cvbounds = self.calendarView.bounds
    let totalSections = monthInfo.count * numberOfSectionsPerMonth
    var page = min(currentSectionPage + 1, totalSections - 1)
    var offsetx = self.calendarView.contentOffset.x
    var offsety = self.calendarView.contentOffset.y
    if self.direction == .Horizontal {
        offsetx = CGFloat(page) * cvbounds.size.width
    } else {
        offsety = CGFloat(page) * cvbounds.size.height
    }
    UIView.animateWithDuration(0.4, animations: {
        self.calendarView.contentOffset = CGPointMake(offsetx, offsety)
        }, completion: { done in
            completion()
    })
}
public func scrollToPreviousSection(completion: () -> Void) {
    let cvbounds = self.calendarView.bounds
    let totalSections = monthInfo.count * numberOfSectionsPerMonth
    var page = max(currentSectionPage - 1, 0)
    var offsetx = self.calendarView.contentOffset.x
    var offsety = self.calendarView.contentOffset.y
    if self.direction == .Horizontal {
        offsetx = CGFloat(page) * cvbounds.size.width
    } else {
        offsety = CGFloat(page) * cvbounds.size.height
    }
    UIView.animateWithDuration(0.4, animations: {
        self.calendarView.contentOffset = CGPointMake(offsetx, offsety)
        }, completion: { done in
            completion()
    })
}

`

But when animating, all the cells from current month disappear... it's not as nice as doing it by scroll...

There's definitely a better option ?!

@BaptisteSansierra
Copy link
Contributor Author

Ok sorry, i missed the funcs scrollToNextSegment / scrollToPreviousSegment, which are doing this very well, but the completion handler was never used, i fixed it that way :

    public func scrollToNextSegment(animateScroll: Bool = true, completionHandler:(()->Void)? = nil) {
        let page = currentSectionPage
        if page + 1 < monthInfo.count {
            let position: UICollectionViewScrollPosition = self.direction == .Horizontal ? .Left : .Top
            if let validHandler = completionHandler {
              delayedExecutionClosure.append(validHandler)
            }
            calendarView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection:page + 1), atScrollPosition: position, animated: animateScroll)
        }
    }
    public func scrollToPreviousSegment(animateScroll: Bool = true, completionHandler:(()->Void)? = nil) {
        let page = currentSectionPage
        if page - 1 > -1 {
            let position: UICollectionViewScrollPosition = self.direction == .Horizontal ? .Left : .Top
            if let validHandler = completionHandler {
              delayedExecutionClosure.append(validHandler)
            }
            calendarView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection:page - 1), atScrollPosition: position, animated: animateScroll)
        }
    }

@BaptisteSansierra
Copy link
Contributor Author

(just created a pull request)

@patchthecode
Copy link
Owner

Just went through your code. Its neat. Its now merged.
Yea i must have missed that one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants