How to Get Uiscrollview Vertical Direction in Swift

How to get UIScrollView vertical direction in Swift?

If you use an UIScrollView then you can take benefit from the scrollViewDidScroll: function. You need to save the last position (the contentOffset) it have and the update it like in the following way:

// variable to save the last position visited, default to zero
 private var lastContentOffset: CGFloat = 0

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (self.lastContentOffset > scrollView.contentOffset.y) {
// move up
}
else if (self.lastContentOffset < scrollView.contentOffset.y) {
// move down
}

// update the new position acquired
self.lastContentOffset = scrollView.contentOffset.y
print(lastContentOffset)
}

There are other ways of doing it of course this is one of them.

I hope this helps you.

Get default scroll direction for UIScrollView Swift

One solution I can think of in order to get the scroll direction before scrolling is to react accordingly to the type of UIScrollView in question.

So first step check if it is a UIScrollView, UICollectionView or UITableView.

In the case of a UITableView

I think a fair assumption here is that the scrolling direction will be vertical

In the case of a UIScrollView

This one could be tricky but I guess here you would have to compare the content size of the scroll view with the frame of the scrollview as Desdenova suggested and this can suggest if you can scroll horizontally and / or vertically.

In the case of a UICollectionView

This data will lie in the in the layout of the UICollectionView.

For example, I set up a basic UICollectionView in Storyboard using a FlowLayout without any data in it.

UICollectionView UICollectionViewFlowLayout scroll direction

Then I add this code somewhere appropriate

if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
{
if layout.scrollDirection == .horizontal {
print("horizontal")
}
else {
print("vertical")
}
}

This prints out horizontal.

Finding the direction of scrolling in a UIScrollView?

Determining the direction is fairly straightforward, but keep in mind that the direction can change several times over the course of a gesture. For example, if you have a scroll view with paging turned on and the user swipes to go to the next page, the initial direction could be rightward, but if you have bounce turned on, it will briefly be going in no direction at all and then briefly be going leftward.

To determine the direction, you'll need to use the UIScrollView scrollViewDidScroll delegate. In this sample, I created a variable named lastContentOffset which I use to compare the current content offset with the previous one. If it's greater, then the scrollView is scrolling right. If it's less then the scrollView is scrolling left:

// somewhere in the private class extension
@property (nonatomic, assign) CGFloat lastContentOffset;

// somewhere in the class implementation
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

ScrollDirection scrollDirection;

if (self.lastContentOffset > scrollView.contentOffset.x) {
scrollDirection = ScrollDirectionRight;
} else if (self.lastContentOffset < scrollView.contentOffset.x) {
scrollDirection = ScrollDirectionLeft;
}

self.lastContentOffset = scrollView.contentOffset.x;

// do whatever you need to with scrollDirection here.
}

I'm using the following enum to define direction. Setting the first value to ScrollDirectionNone has the added benefit of making that direction the default when initializing variables:

typedef NS_ENUM(NSInteger, ScrollDirection) {
ScrollDirectionNone,
ScrollDirectionRight,
ScrollDirectionLeft,
ScrollDirectionUp,
ScrollDirectionDown,
ScrollDirectionCrazy,
};

Set UIScrollview Scroll Direction

You can set the content offset of the scroll view to the bottom. i.e.,

myScroll.contentOffset = CGPoint(x: 0,y: 190) // Here 190 is the same value that represent the height increase done in contentSize.

Detect direction of UIScrollView scroll in scrollViewWillBeginDragging

Thank you, Kris. This is what worked for me, finally:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Detect the scroll direction
if (lastContentOffset < (int)scrollView.contentOffset.x) {
...
}
}

How to detect scrollView scrolling direction?

Check these delegate methods

CGPoint _lastContentOffset;

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

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

if (_lastContentOffset.x < (int)scrollView.contentOffset.x) {
NSLog(@"Scrolled Right");
}
else if (_lastContentOffset.x > (int)scrollView.contentOffset.x) {
NSLog(@"Scrolled Left");
}

else if (_lastContentOffset.y < scrollView.contentOffset.y) {
NSLog(@"Scrolled Down");
}

else if (_lastContentOffset.y > scrollView.contentOffset.y) {
NSLog(@"Scrolled Up");
}
}

UIScrollView should scroll only downwards

Solution

Subclass UIScrollView and override the methods to restrict horizontal scrolling and only scroll if direction is downwards:

class DownwardsOnlyScrollView: UIScrollView
{
override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
// restrict movement to vertical only
let newOffset = CGPoint(x: 0, y: contentOffset.y)

//only scroll if scroll direction is downwards
if newOffset.y > self.contentOffset.y
{
super.setContentOffset(newOffset, animated: animated)
}
}
}

UIScrollView AutoLayout Vertical Only

For disabling the horizontal scroll, you can set the content size in the (void)scrollViewDidScroll method.

[self.scrollView setContentOffset: CGPointMake(0, self.scrollView.contentOffset.y)];

You'll also want to set the directional lock so only 1 scroll direction is used at a time. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/index.html#//apple_ref/occ/instp/UIScrollView/directionalLockEnabled

self.scrollView.directionalLockEnabled = YES;

Vertical UIScrollView over horizontal UIScrollView

Here's a brief method that may help you out:

Obj-C:

create scroll view that is invisible, and then pass the offset of the invisible scroll view or the touch events to the scroll views below depending on the offset of the touch events on the top invisible scroll view that covers the two underlying scroll views:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
[_verticalScrollView setContentOffset:CGPointMake(0, _insivibleScrollView.contentOffset.y)];
[_horizontalScrollView setContentOffset:CGPointMake(_insivibleScrollView.contentOffset.x, 0)];
}

You create an invisible scroll view, and by invisible, I mean you may have to keep the alpha value at like 0.01. You don't allow user interaction for the horizontal and vertical scroll views, but instead use the method above so that when the user touches the invisible scroll view you translate those touches to the scroll views below that are locked down to respond to only the offset of the invisible scroll view. As you've stated in your comment, there's probably more to this answer for your individual needs, but this is the basic "foundation" that will give you the effect that you probably want.

Swift:

Same method as above, but here's the function that you'll need:

func scrollViewDidScroll(scrollView: UIScrollView!) {
verticalScrollView.contentOffset = CGPointMake(0, invisibleScrollView.contentOffset.y)
horizontalScrollView.contentOffset = CGPointMake(invisibleScrollView.contentOffset.x, 0)
}

how to change direction of scrolling in UIScrollView?

the answer was in this link
I had to add these lines to my code in swift 4:

myScroll.transform = CGAffineTransform(rotationAngle: .pi);
subview.transform = CGAffineTransform(rotationAngle: .pi);


Related Topics



Leave a reply



Submit