Skip to content

Commit

Permalink
fix scrolling bug when section title enabled.
Browse files Browse the repository at this point in the history
fix bug of -triggerRefresh not auto showing Activity indicator.
enable section title in demo project.
add trigger refresh button in demo project.
  • Loading branch information
simpzan authored and simpzan committed Jun 23, 2012
1 parent 4c3c88b commit 4f749f0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Demo/SVPullToRefreshDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "SVPullToRefreshDemo/SVPullToRefreshDemo-Prefix.pch";
INFOPLIST_FILE = "SVPullToRefreshDemo/SVPullToRefreshDemo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -280,6 +281,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "SVPullToRefreshDemo/SVPullToRefreshDemo-Prefix.pch";
INFOPLIST_FILE = "SVPullToRefreshDemo/SVPullToRefreshDemo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand Down
14 changes: 12 additions & 2 deletions Demo/SVPullToRefreshDemo/SVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ @interface SVViewController () <UITableViewDelegate, UITableViewDataSource>
@implementation SVViewController
@synthesize tableView = tableView;

- (IBAction)triggerRefresh:(id)sender {
[self.tableView.pullToRefreshView triggerRefresh];
}

- (void)viewDidLoad {
[super viewDidLoad];

Expand Down Expand Up @@ -43,17 +47,18 @@ - (void)viewDidLoad {

// you can temporarily toggle the pull to refresh view
// tableView.showsPullToRefresh = NO;

}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Expand All @@ -65,4 +70,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [NSString stringWithFormat:@"Section %d", section];
}

@end
75 changes: 62 additions & 13 deletions Demo/SVPullToRefreshDemo/en.lproj/SVViewController.xib
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="8.00">
<data>
<int key="IBDocument.SystemTarget">1296</int>
<string key="IBDocument.SystemVersion">11D50b</string>
<string key="IBDocument.InterfaceBuilderVersion">2182</string>
<string key="IBDocument.AppKitVersion">1138.32</string>
<string key="IBDocument.HIToolboxVersion">568.00</string>
<int key="IBDocument.SystemTarget">1280</int>
<string key="IBDocument.SystemVersion">10J3331a</string>
<string key="IBDocument.InterfaceBuilderVersion">1938</string>
<string key="IBDocument.AppKitVersion">1038.35</string>
<string key="IBDocument.HIToolboxVersion">461.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="NS.object.0">1179</string>
<string key="NS.object.0">933</string>
</object>
<array key="IBDocument.IntegratedClassDependencies">
<string>IBProxyObject</string>
<string>IBUIView</string>
<string>IBUITableView</string>
<string>IBUIButton</string>
<string>IBUIView</string>
<string>IBProxyObject</string>
</array>
<array key="IBDocument.PluginDependencies">
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
Expand Down Expand Up @@ -41,8 +42,8 @@
<string key="NSFrameSize">{320, 460}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSWindow"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<object class="NSColor" key="IBUIBackgroundColor">
<reference key="NSNextKeyView" ref="822071720"/>
<object class="NSColor" key="IBUIBackgroundColor" id="307273594">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MQA</bytes>
</object>
Expand All @@ -56,6 +57,38 @@
<float key="IBUISectionHeaderHeight">22</float>
<float key="IBUISectionFooterHeight">22</float>
</object>
<object class="IBUIButton" id="822071720">
<reference key="NSNextResponder" ref="774585933"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{164, 201}, {136, 37}}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView"/>
<bool key="IBUIOpaque">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<int key="IBUIContentHorizontalAlignment">0</int>
<int key="IBUIContentVerticalAlignment">0</int>
<int key="IBUIButtonType">1</int>
<string key="IBUINormalTitle">Trigger Refresh</string>
<reference key="IBUIHighlightedTitleColor" ref="307273594"/>
<object class="NSColor" key="IBUINormalTitleColor">
<int key="NSColorSpace">1</int>
<bytes key="NSRGB">MC4xOTYwNzg0MzQ2IDAuMzA5ODAzOTMyOSAwLjUyMTU2ODY1NgA</bytes>
</object>
<object class="NSColor" key="IBUINormalTitleShadowColor">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MC41AA</bytes>
</object>
<object class="IBUIFontDescription" key="IBUIFontDescription">
<int key="type">2</int>
<double key="pointSize">15</double>
</object>
<object class="NSFont" key="IBUIFont">
<string key="NSName">Helvetica-Bold</string>
<double key="NSSize">15</double>
<int key="NSfFlags">16</int>
</object>
</object>
</array>
<string key="NSFrame">{{0, 20}, {320, 460}}</string>
<reference key="NSSuperview"/>
Expand Down Expand Up @@ -107,6 +140,15 @@
</object>
<int key="connectionID">10</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
<string key="label">triggerRefresh:</string>
<reference key="source" ref="822071720"/>
<reference key="destination" ref="372490531"/>
<int key="IBEventType">7</int>
</object>
<int key="connectionID">14</int>
</object>
</array>
<object class="IBMutableOrderedSet" key="objectRecords">
<array key="orderedObjects">
Expand All @@ -132,6 +174,7 @@
<reference key="object" ref="774585933"/>
<array class="NSMutableArray" key="children">
<reference ref="215774682"/>
<reference ref="822071720"/>
</array>
<reference key="parent" ref="0"/>
</object>
Expand All @@ -140,21 +183,27 @@
<reference key="object" ref="215774682"/>
<reference key="parent" ref="774585933"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">13</int>
<reference key="object" ref="822071720"/>
<reference key="parent" ref="774585933"/>
</object>
</array>
</object>
<dictionary class="NSMutableDictionary" key="flattenedProperties">
<string key="-1.CustomClassName">SVViewController</string>
<string key="-1.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="-2.CustomClassName">UIResponder</string>
<string key="-2.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="13.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="6.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="8.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
</dictionary>
<dictionary class="NSMutableDictionary" key="unlocalizedProperties"/>
<nil key="activeLocalization"/>
<dictionary class="NSMutableDictionary" key="localizations"/>
<nil key="sourceID"/>
<int key="maxID">12</int>
<int key="maxID">14</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
Expand Down Expand Up @@ -183,10 +232,10 @@
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS</string>
<real value="1296" key="NS.object.0"/>
<real value="1280" key="NS.object.0"/>
</object>
<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
<int key="IBDocument.defaultPropertyAccessControl">3</int>
<string key="IBCocoaTouchPluginVersion">1179</string>
<string key="IBCocoaTouchPluginVersion">933</string>
</data>
</archive>
29 changes: 18 additions & 11 deletions SVPullToRefresh/SVPullToRefresh.m
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,23 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}

- (void)scrollViewDidScroll:(CGPoint)contentOffset {
if(pullToRefreshActionHandler && self.state != SVPullToRefreshStateLoading) {
CGFloat scrollOffsetThreshold = self.frame.origin.y-self.originalScrollViewContentInset.top;

if(!self.scrollView.isDragging && self.state == SVPullToRefreshStateTriggered)
self.state = SVPullToRefreshStateLoading;
else if(contentOffset.y > scrollOffsetThreshold && contentOffset.y < -self.originalScrollViewContentInset.top && self.scrollView.isDragging && self.state != SVPullToRefreshStateLoading)
self.state = SVPullToRefreshStateVisible;
else if(contentOffset.y < scrollOffsetThreshold && self.scrollView.isDragging && self.state == SVPullToRefreshStateVisible)
self.state = SVPullToRefreshStateTriggered;
else if(contentOffset.y >= -self.originalScrollViewContentInset.top && self.state != SVPullToRefreshStateHidden)
self.state = SVPullToRefreshStateHidden;
if(pullToRefreshActionHandler) {
if (self.state == SVPullToRefreshStateLoading) {
CGFloat offset = MAX(self.scrollView.contentOffset.y * -1, 0);
offset = MIN(offset, 60.0f);
self.scrollView.contentInset = UIEdgeInsetsMake(offset, 0.0f, 0.0f, 0.0f);

This comment has been minimized.

Copy link
@fphilipe

fphilipe Jul 2, 2012

Contributor

This doesn't respect the original content inset, thus screwing up the view. Why is this even needed?

This comment has been minimized.

Copy link
@simpzan

simpzan Jul 3, 2012

this code is used to fix section title position problem when scrolling on the loading state.

This comment has been minimized.

Copy link
@fphilipe

fphilipe Jul 3, 2012

Contributor

Would the section titles still work by using originalScrollViewContentInset.top + 60 instead of just 60?

This comment has been minimized.

Copy link
@simpzan

simpzan Jul 3, 2012

fine, it works using
originalScrollViewContentInset.top + 60.

} else {
CGFloat scrollOffsetThreshold = self.frame.origin.y-self.originalScrollViewContentInset.top;

if(!self.scrollView.isDragging && self.state == SVPullToRefreshStateTriggered)
self.state = SVPullToRefreshStateLoading;
else if(contentOffset.y > scrollOffsetThreshold && contentOffset.y < -self.originalScrollViewContentInset.top && self.scrollView.isDragging && self.state != SVPullToRefreshStateLoading)
self.state = SVPullToRefreshStateVisible;
else if(contentOffset.y < scrollOffsetThreshold && self.scrollView.isDragging && self.state == SVPullToRefreshStateVisible)
self.state = SVPullToRefreshStateTriggered;
else if(contentOffset.y >= -self.originalScrollViewContentInset.top && self.state != SVPullToRefreshStateHidden)
self.state = SVPullToRefreshStateHidden;
}
}
else if(infiniteScrollingActionHandler) {
CGFloat scrollOffsetThreshold = self.scrollView.contentSize.height-self.scrollView.bounds.size.height-self.originalScrollViewContentInset.top;
Expand All @@ -305,6 +311,7 @@ - (void)scrollViewDidScroll:(CGPoint)contentOffset {

- (void)triggerRefresh {
self.state = SVPullToRefreshStateLoading;
[self.scrollView setContentOffset:CGPointMake(0, -60.0f) animated:YES];
}

- (void)stopAnimating {
Expand Down

0 comments on commit 4f749f0

Please sign in to comment.