[英]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
theYvalue
的类型为(UIView) -> CGFloat
Can I convert this to a CGFloat?我可以将其转换为 CGFloat 吗?
Thank you!谢谢!
let theYvalue = theScrollView.scrollPositionY
Just assigns the function to theYvalue只是将函数分配给 Yvalue
I think what you want is我想你想要的是
let theYvalue = theScrollView.scrollPositionY()
which evaluates the function and should be a CGFloat, which the function returns.它评估函数并且应该是函数返回的 CGFloat。
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
.使用()
调用函数,并且因为您有参数,所以需要传入参数,例如(view: someViewPassedIn)
其中someViewPassedIn
必须是UIView
类型。
In Swift, the difference between functions and closures is very small.在 Swift 中,函数和闭包之间的区别非常小。 Variables can be set as a closure, which is what (UIView) -> CGFloat
is.变量可以设置为一个闭包,这就是(UIView) -> CGFloat
是什么。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.