简体   繁体   中英

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!

let theYvalue = theScrollView.scrollPositionY

Just assigns the function to theYvalue

I think what you want is

let theYvalue = theScrollView.scrollPositionY()

which evaluates the function and should be a CGFloat, which the function returns.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM