Search code examples
iosswiftxcodeuiscrollviewuiwebview

Detect is user has scrolled on iOS and then change colour of view


I am looking for a way in Xcode and Swift to detect if my user has scrolled down the app.

If they have I would like to turn the view at the top of the app from transparent to black. If the user scrolls back to the top again, the view changes from black back to transparent.

Is this something which is achievable in Swift and Xcode?

Thanks in advance! :)

UPDATE Sorry I forgot to mention, I am using uiwebview, with my view overlaying above it. At the top of the app it will be transparent and when I scroll down the webview it will change to black. Can I still detect the scroll the same way?


Solution

  • Yes it is possible in swift. You have to use UIScrollViewDelegate methods to control the scrolling.

     func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    
        print("scrollViewWillBeginDecelerating")
    
        let actualPosition = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
        if (actualPosition.y > 0){
            // Dragging down
    
            UIView.animate(withDuration: 3, animations: { 
               //Change the color of view
            })
        }else{
            // Dragging up
    
            UIView.animate(withDuration: 3, animations: {
                //Change the color of view
            })
        }
    }