Reputation: 11
I am trying to understand how this custom Navigation bar / Paging View works, found here. What is tripping me up when I went through the README was setting up the tinder-like custom behavior:
// Tinder Like
controller?.pagingViewMoving = ({ subviews in
for v in subviews {
var lbl = v as UIImageView
var c = gray
if(lbl.frame.origin.x > 45 && lbl.frame.origin.x < 145) {
c = self.gradient(Double(lbl.frame.origin.x), topX: Double(46), bottomX: Double(144), initC: orange, goal: gray)
}
else if (lbl.frame.origin.x > 145 && lbl.frame.origin.x < 245) {
c = self.gradient(Double(lbl.frame.origin.x), topX: Double(146), bottomX: Double(244), initC: gray, goal: orange)
}
else if(lbl.frame.origin.x == 145){
c = orange
}
lbl.tintColor = c
}
})
I don't understand why there are parentheses around the closure that is being set to the controller?.pagingViewMoving property.
When I look in the SLPagingViewSwift.swift file, the .pagingViewMoving property is set to this alias:
public typealias SLPagingViewMoving = ((subviews: [UIView])-> ())
What are the extra set of parentheses doing outside the function type?
Upvotes: 1
Views: 462
Reputation: 2961
In this case the parentheses are purely for clarity and are unnecessary. Its just like saying let a = (2 + 2)
. Although this should not be confused with closures provided as arguments in the following case.
If a function itself takes an argument which is a closure, the closure is simply within the parentheses thats contain the functions parameters.
So thanks to the syntax higher order functions that take a closure as their last (or only) argument that be represented in two ways - inside the argument parentheses or as whats known as a trailing closure. Consider the following function:
func foo(bar: (Int -> Int)) {...}
it can be called in two ways - first with the closure in the parentheses like so:
foo({(i) in i + 2})
or as a trailing closure:
foo {(i) in i + 2}
Upvotes: 5