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

Make the pull-to-refresh distance change based on superview #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions EGOTableViewPullRefresh/Classes/View/EGORefreshTableHeaderView.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ - (id)initWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UI

self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 30.0f, self.frame.size.width, 20.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 30.0f, self.frame.size.width, 20.0f)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.font = [UIFont systemFontOfSize:12.0f];
label.textColor = textColor;
Expand All @@ -71,7 +71,7 @@ - (id)initWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UI
[label release];

CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(25.0f, frame.size.height - 65.0f, 30.0f, 55.0f);
layer.frame = [self makeArrowFrameForContainer:frame];
layer.contentsGravity = kCAGravityResizeAspect;
layer.contents = (id)[UIImage imageNamed:arrow].CGImage;

Expand Down Expand Up @@ -99,8 +99,24 @@ - (id)initWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UI

}

- (CGRect)makeArrowFrameForContainer:(CGRect)frame {
CGFloat arrowY = frame.size.height - 65.0f;
float arrowHeight = 55.0f;
if(frame.size.height < 65){
arrowY = 10;
arrowHeight = frame.size.height - 20;
}
CGRect arrowFrame = CGRectMake(25.0f, arrowY, 30.0f, arrowHeight);
return arrowFrame;
}

- (float)getPullDistance {
return self.superview.frame.size.height/4;
// return 65.0f;
}

- (id)initWithFrame:(CGRect)frame {
return [self initWithFrame:frame arrowImageName:@"blueArrow.png" textColor:TEXT_COLOR];
return [self initWithFrame:frame arrowImageName:@"blueArrow.png" textColor:TEXT_COLOR];
}

#pragma mark -
Expand All @@ -116,7 +132,7 @@ - (void)refreshLastUpdatedDate {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];

_lastUpdatedLabel.text = [NSString stringWithFormat:@"Last Updated: %@", [dateFormatter stringFromDate:date]];
[[NSUserDefaults standardUserDefaults] setObject:_lastUpdatedLabel.text forKey:@"EGORefreshTableView_LastRefresh"];
[[NSUserDefaults standardUserDefaults] synchronize];
Expand All @@ -126,7 +142,7 @@ - (void)refreshLastUpdatedDate {
_lastUpdatedLabel.text = nil;

}

}

- (void)setState:(EGOPullRefreshState)aState{
Expand All @@ -153,7 +169,7 @@ - (void)setState:(EGOPullRefreshState)aState{
_statusLabel.text = NSLocalizedString(@"Pull down to refresh...", @"Pull down to refresh status");
[_activityView stopAnimating];
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
_arrowImage.hidden = NO;
_arrowImage.transform = CATransform3DIdentity;
[CATransaction commit];
Expand All @@ -166,7 +182,7 @@ - (void)setState:(EGOPullRefreshState)aState{
_statusLabel.text = NSLocalizedString(@"Loading...", @"Loading Status");
[_activityView startAnimating];
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
_arrowImage.hidden = YES;
[CATransaction commit];

Expand All @@ -182,7 +198,7 @@ - (void)setState:(EGOPullRefreshState)aState{
#pragma mark -
#pragma mark ScrollView Methods

- (void)egoRefreshScrollViewDidScroll:(UIScrollView *)scrollView {
- (void)egoRefreshScrollViewDidScroll:(UIScrollView *)scrollView {

if (_state == EGOOPullRefreshLoading) {

Expand All @@ -197,9 +213,9 @@ - (void)egoRefreshScrollViewDidScroll:(UIScrollView *)scrollView {
_loading = [_delegate egoRefreshTableHeaderDataSourceIsLoading:self];
}

if (_state == EGOOPullRefreshPulling && scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f && !_loading) {
if (_state == EGOOPullRefreshPulling && scrollView.contentOffset.y > ([self getPullDistance] * -1) && scrollView.contentOffset.y < 0.0f && !_loading) {
[self setState:EGOOPullRefreshNormal];
} else if (_state == EGOOPullRefreshNormal && scrollView.contentOffset.y < -65.0f && !_loading) {
} else if (_state == EGOOPullRefreshNormal && scrollView.contentOffset.y < ([self getPullDistance] * -1) && !_loading) {
[self setState:EGOOPullRefreshPulling];
}

Expand All @@ -218,7 +234,7 @@ - (void)egoRefreshScrollViewDidEndDragging:(UIScrollView *)scrollView {
_loading = [_delegate egoRefreshTableHeaderDataSourceIsLoading:self];
}

if (scrollView.contentOffset.y <= - 65.0f && !_loading) {
if (scrollView.contentOffset.y <= ([self getPullDistance] * -1) && !_loading) {

if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDidTriggerRefresh:)]) {
[_delegate egoRefreshTableHeaderDidTriggerRefresh:self];
Expand All @@ -234,15 +250,15 @@ - (void)egoRefreshScrollViewDidEndDragging:(UIScrollView *)scrollView {

}

- (void)egoRefreshScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {
- (void)egoRefreshScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
[scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
[UIView commitAnimations];

[self setState:EGOOPullRefreshNormal];

}


Expand Down