How to Detect the Scroll Direction from the Uicollectionview

How can I detect the scroll direction from the UICollectionView?

Try this:

Add this somewhere in you header:

@property (nonatomic) CGFloat lastContentOffset;

Then override the scrollViewDidScroll: method:

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.lastContentOffset > scrollView.contentOffset.y)
{
NSLog(@"Scrolling Up");
}
else if (self.lastContentOffset < scrollView.contentOffset.y)
{
NSLog(@"Scrolling Down");
}

self.lastContentOffset = scrollView.contentOffset.y;
}

Found in Finding the direction of scrolling in a UIScrollView?

How to check in UICollectionViewFlowLayout that in which direction the uicollectionview is scrolling? (Swift)

Add one method with your CustomFlowLayout and with scrollViewDidScroll method call it.

class CustomFlowLayout: UICollectionViewFlowLayout {

var currentScrollDirection = ""

func currentScroll(direction: String) {
currentScrollDirection = direction
}
}

Now implement scrollViewDidScroll method with your ViewController and call the currentScroll method with CustomFlowLayout.

var lastContentOffset = CGPoint.zero
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let currentOffset = scrollView.contentOffset
var scrollDirection = (currentOffset.y > self.lastContentOffset.y) ? "Down" : "Up"
if let flowLayout = collectionView.collectionViewLayout as? CustomFlowLayout {
flowLayout.currentScroll(direction: scrollDirection)
}
self.lastContentOffset = currentOffset
}

Detect direction of scrolling UICollectionView, load data from REST

You can check UIScrollView's (which UICollectionView inherits from) panGestureRecognizer property and do something like this:

CGPoint scrollVelocity = [collectionView.panGestureRecognizer velocityInView:collectionView.superview];
if (scrollVelocity.y > 0.0f) {
NSLog(@"going down");
} else if (scrollVelocity.y < 0.0f) {
NSLog(@"going up");
}

Swift 3.1:

let scrollVelocity = collectionView.panGestureRecognizer.velocityInView(collectionView.superview)
if (scrollVelocity.y > 0.0) {
print("going down")
} else if (scrollVelocity.y < 0.0) {
print("going up")
}

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,
};

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.

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.

How can i get how to scrolled it is in uicollectionView

you can get the scroll event with UIScrollViewDelegate and

func scrollViewDidScroll(_ scrollView: UIScrollView) { }

Now, you can have the scroll position with scrollView.contentOffset it's a CGPoint, so with a conversion (point to pixel) you should get the numbers of Pixel.

scrollView.contentOffset.y * UIScreen.main.scale

This should works.

How can I change the scroll direction in UICollectionView?

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

Note too that it seems to be fine to call this in prepareForLayout in your flow layout...

@interface LayoutHorizontalThings : UICollectionViewFlowLayout
@end

@implementation LayoutHorizontalBooks
-(void)prepareLayout
{
[super prepareLayout];

self.scrollDirection = UICollectionViewScrollDirectionHorizontal;

self.minimumInteritemSpacing = 0;
self.minimumLineSpacing = 0;
self.itemSize = CGSizeMake(110,130);
self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

Swift 4 UICollectionView detect end of scrolling

You may use cellForItem or willDisplayItem methods of collection view. Check if the last cell is being displayed, and load your data. For example:

Swift:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if (indexPath.row == dataSource.count - 1 ) { //it's your last cell
//Load more data & reload your collection view
}
}

Objective-C:

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == dataSource.count - 1 ) { //it's your last cell
//Load more data & reload your collection view

}
}


Related Topics



Leave a reply



Submit