user4532193
user4532193

Reputation:

xCode - add scroll event to UIScrollView

I wrote an function that should do something when the top or bottom is hit with the UIScrollView.

    -(void)scrollViewDidScroll: (UIScrollView*)scrolling
{
    NSLog(@"Event triggered.");
    float scrollViewHeight = scrolling.frame.size.height;
    float scrollContentSizeHeight = scrolling.contentSize.height;
    float scrollOffset = scrolling.contentOffset.y;

        if (scrollOffset == 0)
        {
            _lol.text = @"top hit";
            NSLog(@"Top Hit");
        }
        else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
        {
            _lol.text = @"bottom hit";
            NSLog(@"bottom Hit");
        }
    }

The problem is: I have no idea how to link this function to my UIScrollView named "scrolling".

Thanks in advance.

Upvotes: 0

Views: 2485

Answers (2)

Fahim Parkar
Fahim Parkar

Reputation: 31637

Let's say you have scrollview named mScrollView1

To call use it as below

 [self scrollViewDidScroll:mScrollView1];
                           ^^^^^^^^^^^^ here is your scrollview name

Edit 1

This is what is used to handles all the edge cases for scrollview. You need an ivar to keep state, and as shown in the comments, there are other ways to handle this.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //[super scrollViewWillBeginDragging:scrollView];   // pull to refresh

    self.isScrolling = YES;
    NSLog(@"+scrollViewWillBeginDragging");
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //[super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];    // pull to refresh

    if(!decelerate) {
        self.isScrolling = NO;
    }
    NSLog(@"%@scrollViewDidEndDragging", self.isScrolling ? @"" : @"-");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.isScrolling = NO;
    NSLog(@"-scrollViewDidEndDecelerating");
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{   
    self.isScrolling = NO;
    NSLog(@"-scrollViewDidScrollToTop");
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    self.isScrolling = NO;
    NSLog(@"-scrollViewDidEndScrollingAnimation");
}

Now use the appropriate delegate and call your method.

Don't forget to add delegate to your scrollview.


Edit 2

To add delegate for scrollview, write below line

myScrollview.delegate = self;

Upvotes: 3

Saidre
Saidre

Reputation: 56

- (void)scrollViewDidScroll:(UIScrollView *)scrollView is a UIScrollViewDelegate's method. It will be called automaticaly.
For that, you have to set the delegate for your UIScrollView as the current ViewController. Set this in your (void)viewDidLoad :
self.myScrollView.delegate = self.
And on the .h your View Controller has to implement the Protocol <UIScrollViewDelegate>: @interface MyViewController : UIViewController <UIScrollViewDelegate>.
Now include the method -(void)scrollViewDidScroll: (UIScrollView*)scrolling on the .m and make the implementation.

Upvotes: 0

Related Questions