iosswiftuiscrollviewcgfloat

What is a "(UIView) -> CGFloat"? I am trying to get a CGFloat value


extension UIScrollView {
    func scrollPositionY(view:UIView) -> CGFloat {
        if let origin = view.superview {
            // Get the Y position of your child view
            let childStartPoint = origin.convert(view.frame.origin, to: self)

            let this = childStartPoint.y

            return this
        }
    }
}

and then

let theYvalue = theScrollView.scrollPositionY

theYvalue is of type (UIView) -> CGFloat

Can I convert this to a CGFloat?

Thank you!


Solution

  • You didn't call the function. Functions are called using (), and because you have parameters, arguments need to be passed in, like (view: someViewPassedIn) where someViewPassedIn must be of type UIView.

    In Swift, the difference between functions and closures is very small. Variables can be set as a closure, which is what (UIView) -> CGFloat is. It is a closure that has not been called like a function.

    It can be quite hard to wrap your head around, but basically closures contain the block of code between the curly brackets/braces, {}, and the () runs that block of code.

    Instead of:

    let theYvalue = theScrollView.scrollPositionY
    

    Try:

    let theYvalue = theScrollView.scrollPositionY(view: someViewPassedIn)