0

I have a UIViewController with a UIScrollView I've added in Interface Builder. I want to have scrollView functions so I can detect if it's been scrolled up or down. My scroll view is attached with an IBOutlet called "mainScroll." Here is my code:

var lastContentOffset: CGFloat = 0
@IBOutlet weak var mainScroll: UIScrollView!

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.lastContentOffset = mainScroll.contentOffset.y
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (self.lastContentOffset < mainScroll.contentOffset.y) {
        // did move up
        print("Did Move Up")
    } else if (self.lastContentOffset > mainScroll.contentOffset.y) {
        // did move down
        print("Did Move Down")
    } else {
        // didn't move
    }
}

I've added UIScrollViewDelegate to my class. I have a feeling I'm not using the scrollView delegate callbacks properly. How can I take these functions and assign them to my "mainScroll"?

2
  • 1
    You need to connect the scroll view's delegate in the storyboard. Commented Feb 22, 2019 at 0:45
  • Thanks for this, I knew I was missing something Commented Feb 22, 2019 at 0:51

1 Answer 1

1

May be you need this in viewDidLoad

mainScroll.delegate = self
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.