Skip to content

Commit

Permalink
Updated final fix for version 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
patchthecode committed Jun 5, 2016
1 parent c4c3cfb commit 2365e61
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 81 deletions.
33 changes: 13 additions & 20 deletions Example/JTAppleCalendar/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -79,55 +79,48 @@
<stackView opaque="NO" contentMode="scaleToFill" misplaced="YES" axis="vertical" distribution="fillEqually" alignment="top" translatesAutoresizingMaskIntoConstraints="NO" id="iQI-Hq-aUc">
<rect key="frame" x="16" y="14925" width="157" height="150"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nfk-D6-bAe">
<rect key="frame" x="0.0" y="0.0" width="47" height="22"/>
<state key="normal" title="Reload"/>
<connections>
<action selector="reloadCal" destination="BYZ-38-t0r" eventType="touchUpInside" id="asI-2d-7Db"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="rWO-5s-mKQ">
<rect key="frame" x="0.0" y="22" width="107" height="22"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="rWO-5s-mKQ">
<rect key="frame" x="0.0" y="0.0" width="107" height="25"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Change to 3 rows"/>
<connections>
<action selector="changeToThreeRows:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Wb1-go-yPa"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" verticalCompressionResistancePriority="751" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="13p-cR-5Oy">
<rect key="frame" x="0.0" y="43" width="110" height="22"/>
<button opaque="NO" contentMode="scaleToFill" verticalCompressionResistancePriority="751" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="13p-cR-5Oy">
<rect key="frame" x="0.0" y="25" width="110" height="25"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Change to 6 Rows"/>
<connections>
<action selector="changeToSixRows:" destination="BYZ-38-t0r" eventType="touchUpInside" id="YTT-Xm-Hgz"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="EZB-eU-i2Z">
<rect key="frame" x="0.0" y="65" width="146" height="21"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="EZB-eU-i2Z">
<rect key="frame" x="0.0" y="50" width="146" height="25"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Select many 2016 dates"/>
<connections>
<action selector="select10:" destination="BYZ-38-t0r" eventType="touchUpInside" id="ce9-dQ-iNd"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="WaY-fY-0lz">
<rect key="frame" x="0.0" y="86" width="126" height="22"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="WaY-fY-0lz">
<rect key="frame" x="0.0" y="75" width="126" height="25"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Select 11th Feb 2016"/>
<connections>
<action selector="select11:" destination="BYZ-38-t0r" eventType="touchUpInside" id="2hr-AE-WhN"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4fl-ZS-PXq">
<rect key="frame" x="0.0" y="107" width="139" height="22"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4fl-ZS-PXq">
<rect key="frame" x="0.0" y="100" width="139" height="25"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Scroll to 11th Mar 2016"/>
<connections>
<action selector="scrollToDate:" destination="BYZ-38-t0r" eventType="touchUpInside" id="WYt-WI-pJk"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="X5M-bv-yCY">
<rect key="frame" x="0.0" y="129" width="123" height="22"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="X5M-bv-yCY">
<rect key="frame" x="0.0" y="125" width="123" height="25"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="Print Selected dates"/>
<connections>
Expand Down Expand Up @@ -250,8 +243,8 @@
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="SnB-SU-fZX"/>
<exclude reference="jXl-ia-jmV"/>
<exclude reference="SnB-SU-fZX"/>
</mask>
</variation>
</view>
Expand Down
18 changes: 5 additions & 13 deletions Example/JTAppleCalendar/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class ViewController: UIViewController {
// Enable the following code line to show headers. There are other lines of code to uncomment as well
calendarView.registerHeaderViewXibs(fileNames: ["PinkSectionHeaderView", "WhiteSectionHeaderView"]) // headers are Optional. You can register multiple if you want.


// The following default code can be removed since they are already the default.
// They are only included here so that you can know what properties can be configured
calendarView.direction = .Horizontal // default is horizontal
Expand All @@ -60,9 +59,11 @@ class ViewController: UIViewController {
calendarView.scrollResistance = 0.75 // default is 0.75 - this is only applicable when paging is not enabled.
calendarView.reloadData()

// After reloading. Scroll to your selected date, and setup your calendar views
let currentDate = self.calendarView.currentCalendarDateSegment()
self.setupViewsOfCalendar(currentDate.startDate, endDate: currentDate.endDate)
// After reloading. Scroll to your selected date, and setup your calendar
calendarView.scrollToDate(NSDate(), triggerScrollToDateDelegate: false, animateScroll: false) {
let currentDate = self.calendarView.currentCalendarDateSegment()
self.setupViewsOfCalendar(currentDate.startDate, endDate: currentDate.endDate)
}
}

@IBAction func select10(sender: AnyObject?) {
Expand All @@ -78,15 +79,6 @@ class ViewController: UIViewController {
calendarView.selectDates(dates, triggerSelectionDelegate: false)
}

@IBAction func reloadCal() {
monthLabel.text = ""
calendarView.reloadData()

let currentDate = self.calendarView.currentCalendarDateSegment()
self.setupViewsOfCalendar(currentDate.startDate, endDate: currentDate.endDate)

}

@IBAction func select11(sender: AnyObject?) {
calendarView.allowsMultipleSelection = false
let date = formatter.dateFromString("2016 02 11")
Expand Down
6 changes: 5 additions & 1 deletion Pod/Classes/JTAppleCalendarDelegates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,18 @@ extension JTAppleCalendarView: UIScrollViewDelegate {

/// Tells the delegate when a scrolling animation in the scroll view concludes.
public func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
if triggerScrollToDateDelegate {
if let shouldTrigger = triggerScrollToDateDelegate where shouldTrigger == true {
scrollViewDidEndDecelerating(scrollView)
triggerScrollToDateDelegate = nil
}

executeDelayedTasks()

// Update the focus item whenever scrolled
(calendarView.collectionViewLayout as! JTAppleCalendarLayoutProtocol).pointForFocusItem = scrollView.contentOffset

// A scroll was just completed.
scrollInProgress = false
}

/// Tells the delegate that the scroll view has ended decelerating the scrolling movement.
Expand Down
64 changes: 45 additions & 19 deletions Pod/Classes/JTAppleCalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public class JTAppleCalendarView: UIView {
}
}

var triggerScrollToDateDelegate = true
var triggerScrollToDateDelegate: Bool? = true

// Keeps track of item size for a section. This is an optimization
var indexPathSectionItemSize: (section: Int, itemSize: CGSize)?
Expand Down Expand Up @@ -432,43 +432,69 @@ public class JTAppleCalendarView: UIView {
return nil
}

func scrollToHeaderInSection(section:Int, animated: Bool = true) {
func scrollToHeaderInSection(section:Int, triggerScrollToDateDelegate: Bool = false, withAnimation animation: Bool = true, completionHandler: (()->Void)? = nil) {
if headerViewXibs.count < 1 { return }

self.triggerScrollToDateDelegate = triggerScrollToDateDelegate

let indexPath = NSIndexPath(forItem: 0, inSection: section)
calendarView.layoutIfNeeded()
if let attributes = calendarView.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) {
if let validHandler = completionHandler {
delayedExecutionClosure.append(validHandler)
}

let topOfHeader = CGPointMake(0, attributes.frame.origin.y - calendarView.contentInset.top)
delayRunOnMainThread(0.0, closure: {
self.calendarView.setContentOffset(topOfHeader, animated:animated)
let topOfHeader = CGPointMake(attributes.frame.origin.x, attributes.frame.origin.y)
self.scrollInProgress = true
delayRunOnMainThread(0.0, closure: {
self.calendarView.setContentOffset(topOfHeader, animated:animation)

delayRunOnMainThread(0.0, closure: {
if !animation {
self.scrollViewDidEndScrollingAnimation(self.calendarView)
self.scrollInProgress = false
} else {
// If the scroll is set to animate, and the target content offset is already on the screen, then the didFinishScrollingAnimation
// delegate will not get called. Once animation is on let's force a scroll so the delegate MUST get caalled
if let check = self.calendarOffsetIsAlreadyAtScrollPosition(forOffset: topOfHeader) where check == true {
self.scrollViewDidEndScrollingAnimation(self.calendarView)
self.scrollInProgress = false
}
}
})
})
}
}

func reloadData(checkDelegateDataSource check: Bool, withAnchorDate anchorDate: NSDate? = nil, completionHandler:(()->Void)? = nil) {
func reloadData(checkDelegateDataSource check: Bool, withAnchorDate anchorDate: NSDate? = nil, withAnimation animation: Bool = false, completionHandler:(()->Void)? = nil) {
if check {
reloadDelegateDataSource() // Reload the datasource
}

// Delay on main thread. We want this to be called after the view is displayed on the main run loop
if layoutNeedsUpdating {
delayRunOnMainThread(0.0, closure: {
self.configureChangeOfRows()

guard let validAnchorDate = anchorDate else { // If the date is invalid just scroll to the the first item on the view or scroll to the start of a header (if header is enabled)
if headerViewXibs.count < 1 {
if !self.scrollInProgress { // Make sure this scroll only gets activated if no other scroll is in queue
self.scrollToDate(self.startOfMonthCache, triggerScrollToDateDelegate: false, animateScroll: false, completionHandler: completionHandler)
if !self.scrollInProgress { // Make sure this scroll only gets activated if no other scroll is in queue
self.configureChangeOfRows()

guard let validAnchorDate = anchorDate else { // If the date is invalid just scroll to the the first item on the view or scroll to the start of a header (if header is enabled)
if headerViewXibs.count < 1 {
self.scrollToDate(self.startOfMonthCache, triggerScrollToDateDelegate: false, animateScroll: animation, completionHandler: completionHandler)
} else {
self.scrollToHeaderForDate(self.startOfMonthCache, triggerScrollToDateDelegate: false, withAnimation: animation, completionHandler: completionHandler)
}
} else {
self.scrollToHeaderForDate(self.startOfMonthCache)
return
}

delayRunOnMainThread(0.0, closure: { () -> () in
self.scrollToDate(validAnchorDate, triggerScrollToDateDelegate: false, animateScroll: animation, completionHandler: completionHandler)
})
} else {
if let validHandler = completionHandler {
self.delayedExecutionClosure.append(validHandler)
}
return
}

delayRunOnMainThread(0.0, closure: { () -> () in
self.scrollToDate(validAnchorDate, triggerScrollToDateDelegate: false, animateScroll: false, completionHandler: completionHandler)
})

})
} else {
(calendarView.collectionViewLayout as! JTAppleCalendarLayoutProtocol).clearCache()
Expand Down
Loading

0 comments on commit 2365e61

Please sign in to comment.