Angolao
Angolao

Reputation: 992

How do I trigger scrollViewWillEndDragging event?

I have a view overlaid on the top of UITableView. But I need to trigger scroll event for the UITableView event while user dragging on the top view.

I added UIPanGestureRecognizer on the view, and trigger the scrollViewDidScroll event successfully while using setContentOffset:

@IBAction func handleScroll(sender: UIPanGestureRecognizer)
{
    if sender.state == UIGestureRecognizerState.Changed
    {
        let transtion = sender.translationInView(self.contentViewContainer)

        tableViewController.tableView.setContentOffset(CGPointMake(0, 0 - transtion.y), animated: false)
    }
}

// UITableView delegate
override func scrollViewDidScroll(scrollView: UIScrollView)
{
    // this will be called while using setContentOffset.
}

But How do I trigger the scrollViewWillEndDragging event for this case?

@IBAction func headerScroll(sender: UIPanGestureRecognizer)
{       
    if sender.state == UIGestureRecognizerState.Ended
    {
        // Do what?
    }
}

override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
{
    // To trigger this?
}

Upvotes: 2

Views: 1545

Answers (5)

Pankaj Taneja
Pankaj Taneja

Reputation: 13

When you set the table view delegate, it internally sets your class to the scroll view delegate as well, you can directly implement

Check this declaration of UITableViewDelegate @protocol UITableViewDelegate

Upvotes: 0

khunshan
khunshan

Reputation: 2862

Why not you track tableview instead of scroll.

tableView:didEndDisplayingCell:forRowAtIndexPath:

This method will be called after each cell is displayed.

Upvotes: 1

user2103679
user2103679

Reputation:

UITableView has a property called 'panGestureRecognizer' (actually it's a UIScrollView property). You can take a reference to it and add it to your overlaid view. In this way you will trigger any UIScrollView related events.

UIPanGestureRecognizer *pan = myTableView.panGestureRecognizer;
[myOverlaidView addGestureRecognizer:pan];

Upvotes: 0

Josh Gafni
Josh Gafni

Reputation: 2881

Instead of trying to trigger scrollViewWillEndDragging, can't you call:

[tableView setContentOffset:tableView.contentOffset animated:NO];

It should stop the table from scrolling.

Upvotes: 0

Dave Batton
Dave Batton

Reputation: 8835

You aren't required to "trigger" delegate methods. You can call scrollViewWillEndDragging() directly.

However, keep in mind that some things like scrolling happen on the main thread, so you probably won't see your updates happen until the user action has actually completed.

Upvotes: 0

Related Questions