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

no diagonal #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions AAPullToRefreshDemo/AAPullToRefreshDemo/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>
{
CGPoint jPreviousTouchPoint;
int jSwipeDirection;
}

@end
78 changes: 78 additions & 0 deletions AAPullToRefreshDemo/AAPullToRefreshDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ @interface ViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@end

typedef enum ScrollDirection {
ScrollDirectionNone,
ScrollDirectionRight,
ScrollDirectionLeft,
ScrollDirectionUp,
ScrollDirectionDown,
ScrollDirectionCrazy,
} ScrollDirection;

//Restricting diagonal scrolling
typedef NS_ENUM(NSInteger, JUSTVANBLOOM_SCROLLDIRECTION) {
eScrollLeft = 0,
eScrollRight = 1,
eScrollTop = 2,
eScrollBottom = 3,
eScrollNone = 4
};

@implementation ViewController

- (void)viewDidLoad
Expand Down Expand Up @@ -84,4 +102,64 @@ - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
return self.thresholdView;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = self.scrollView.contentOffset;

//Cool... Restricting diagonal scrolling
jSwipeDirection = [self getScrollDirection:jPreviousTouchPoint endPoint:self.scrollView.contentOffset];
switch (jSwipeDirection) {
case eScrollLeft:
self.scrollView.contentOffset = CGPointMake(offset.x, jPreviousTouchPoint.y);
break;

case eScrollRight:
self.scrollView.contentOffset = CGPointMake(offset.x, jPreviousTouchPoint.y);
break;

case eScrollTop:
self.scrollView.contentOffset = CGPointMake(jPreviousTouchPoint.x, offset.y);
break;

case eScrollBottom:
self.scrollView.contentOffset = CGPointMake(jPreviousTouchPoint.x, offset.y);
break;

default:
break;
}
}

-(JUSTVANBLOOM_SCROLLDIRECTION) getScrollDirection : (CGPoint) startPoint endPoint:(CGPoint) endPoint
{
JUSTVANBLOOM_SCROLLDIRECTION direction = eScrollNone;

JUSTVANBLOOM_SCROLLDIRECTION xDirection;
JUSTVANBLOOM_SCROLLDIRECTION yDirection;

int xDirectionOffset = startPoint.x - endPoint.x;
if(xDirectionOffset > 0)
xDirection = eScrollLeft;
else
xDirection = eScrollRight;

int yDirectionOffset = startPoint.y - endPoint.y;
if(yDirectionOffset > 0)
yDirection = eScrollTop;
else
yDirection = eScrollBottom;

if(abs(xDirectionOffset) > abs(yDirectionOffset))
direction = xDirection;
else
direction = yDirection;

return direction;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
jPreviousTouchPoint = scrollView.contentOffset;
}

@end