diff --git a/Sources/Classes/Full View/FullViewAnimation.swift b/Sources/Classes/Full View/FullViewAnimation.swift index b003e16..b9565da 100644 --- a/Sources/Classes/Full View/FullViewAnimation.swift +++ b/Sources/Classes/Full View/FullViewAnimation.swift @@ -53,7 +53,7 @@ public extension Spruce { /// - recursiveDepth: an int describing how deep into the view hiearchy the subview search should go, defaults to 0 /// - completion: a closure that is called upon the final animation completing. A `Bool` is passed into the closure letting you know if the animation has completed. **Note:** If you stop animations on the whole animating view, then `false` will be passed into the completion closure. However, if the final animation is allowed to proceed then `true` will be the value passed into the completion closure. public func animate(withSortFunction sortFunction: SortFunction, duration: StockTimingFunction, prepare: PrepareHandler? = nil, animation: Animation, exclude: [UIView]? = nil, recursiveDepth: Int = 0, completion: CompletionHandler? = nil) { - let weightedViews = sortFunction.weights(forView: self.view, recursiveDepth: recursiveDepth) + let weightedViews = sortFunction.weights(for: self.view, recursiveDepth: recursiveDepth) var timedViews = duration.timingFunction.timeOffsets(forViews: weightedViews) timedViews = timedViews.sorted { (left, right) -> Bool in return left.timeOffset < right.timeOffset @@ -76,5 +76,8 @@ public extension Spruce { view: animatedView, completion: ((index == timedViews.count - 1) ? completion : nil)) } + + let array = [1,2,3,4,5] + array.reversed() } } diff --git a/Sources/Classes/Sort Functions/CorneredSortFunction.swift b/Sources/Classes/Sort Functions/CorneredSortFunction.swift index 09d032e..865b6a5 100644 --- a/Sources/Classes/Sort Functions/CorneredSortFunction.swift +++ b/Sources/Classes/Sort Functions/CorneredSortFunction.swift @@ -36,21 +36,21 @@ public struct CorneredSortFunction: CornerSortFunction { self.corner = corner } - public func weights(forView view: UIView, recursiveDepth: Int) -> [WeightedView] { - let comparisonPoint = distancePoint(view: view) + public func weights(for view: UIView, recursiveDepth: Int) -> [WeightedView] { let subviews = view.spruce.subviews(withRecursiveDepth: recursiveDepth) + let comparisonPoint = distancePoint(view: view, subviews: subviews) var maxWeight: Double = 0.0 - let weightedViews: [WeightedView] = subviews.map { + var weightedViews: [WeightedView] = subviews.map { let distance = Double(abs(comparisonPoint.x - $0.referencePoint.x) + abs(comparisonPoint.y - $0.referencePoint.y)) maxWeight = max(maxWeight, distance) return WeightedView(spruceView: $0, weight: distance) } if reversed { - for var weightedView in weightedViews { - weightedView.weight = maxWeight - weightedView.weight + for index in 0.. [WeightedView] { + public func weights(for view: UIView, recursiveDepth: Int) -> [WeightedView] { let subviews = view.spruce.subviews(withRecursiveDepth: recursiveDepth) var currentWeight: Double = 0.0 var weightedViews: [WeightedView] = [] diff --git a/Sources/Classes/Sort Functions/InlineSortFunction.swift b/Sources/Classes/Sort Functions/InlineSortFunction.swift index 1ff716f..181c2d3 100644 --- a/Sources/Classes/Sort Functions/InlineSortFunction.swift +++ b/Sources/Classes/Sort Functions/InlineSortFunction.swift @@ -37,7 +37,7 @@ public struct InlineSortFunction: CornerSortFunction { self.corner = corner } - public func weights(forView view: UIView, recursiveDepth: Int) -> [WeightedView] { + public func weights(for view: UIView, recursiveDepth: Int) -> [WeightedView] { let comparisonPoint = distancePoint(view: view) let subviews = view.spruce.subviews(withRecursiveDepth: recursiveDepth) diff --git a/Sources/Classes/Sort Functions/Protocols/DistanceSortFunction.swift b/Sources/Classes/Sort Functions/Protocols/DistanceSortFunction.swift index acac3e6..7f132b6 100644 --- a/Sources/Classes/Sort Functions/Protocols/DistanceSortFunction.swift +++ b/Sources/Classes/Sort Functions/Protocols/DistanceSortFunction.swift @@ -66,21 +66,21 @@ public extension DistanceSortFunction { /// - view: the view whose subviews should be animated. This view should not be included in the returned array /// - recursiveDepth: an int describing how deep into the view hiearchy the subview search should go, defaults to 0. A value of 0 is the same as calling the `subviews` on the actual view itself. Therefore a depth of 1 will be getting the subviews of each of the subviews, etc... /// - Returns: an array of `WeightedView`'s which contain references to the view needed to be animated and the weight for the animation of that individual view relative to the overall animation - public func weights(forView view: UIView, recursiveDepth: Int) -> [WeightedView] { + public func weights(for view: UIView, recursiveDepth: Int) -> [WeightedView] { let subviews = view.spruce.subviews(withRecursiveDepth: recursiveDepth) let comparisonPoint = distancePoint(view: view, subviews: subviews) var maxWeight: Double = 0.0 - let weightedViews: [WeightedView] = subviews.map { + var weightedViews: [WeightedView] = subviews.map { let distance = distanceBetween(comparisonPoint, and: $0.referencePoint) maxWeight = max(maxWeight, distance) return WeightedView(spruceView: $0, weight: distance) } if reversed { - for var weightedView in weightedViews { - weightedView.weight = maxWeight - weightedView.weight + for index in 0.. [WeightedView] + func weights(for view: UIView) -> [WeightedView] /// Given a view, view sort the subviews in a way that matches the desired specification of the `SortFunction`. In an example case, if you wanted a radial sort function then this method would return an array of the subviews such that their weights would be smaller near the center of the view and grow as they get further from the center point. /// @@ -63,11 +63,11 @@ public protocol SortFunction { /// - view: the view whose subviews should be animated. This view should not be included in the returned array /// - recursiveDepth: an int describing how deep into the view hiearchy the subview search should go, defaults to 0. A value of 0 is the same as calling the `subviews` on the actual view itself. Therefore a depth of 1 will be getting the subviews of each of the subviews, etc... /// - Returns: an array of `WeightedView`'s which contain references to the view needed to be animated and the weight for the animation of that individual view relative to the overall animation - func weights(forView view: UIView, recursiveDepth: Int) -> [WeightedView] + func weights(for view: UIView, recursiveDepth: Int) -> [WeightedView] } public extension SortFunction { - func weights(forView view: UIView) -> [WeightedView] { - return weights(forView: view, recursiveDepth: 0) + func weights(for view: UIView) -> [WeightedView] { + return weights(for: view, recursiveDepth: 0) } } diff --git a/Sources/Classes/Sort Functions/RandomSortFunction.swift b/Sources/Classes/Sort Functions/RandomSortFunction.swift index 3d66e03..af1306a 100644 --- a/Sources/Classes/Sort Functions/RandomSortFunction.swift +++ b/Sources/Classes/Sort Functions/RandomSortFunction.swift @@ -33,7 +33,7 @@ public struct RandomSortFunction: SortFunction { } - public func weights(forView view: UIView, recursiveDepth: Int) -> [WeightedView] { + public func weights(for view: UIView, recursiveDepth: Int) -> [WeightedView] { var subviews = view.spruce.subviews(withRecursiveDepth: recursiveDepth) subviews.shuffle() diff --git a/Sources/Classes/Sort Functions/WeightedSortFunction.swift b/Sources/Classes/Sort Functions/WeightedSortFunction.swift index c8ff929..6d01e28 100644 --- a/Sources/Classes/Sort Functions/WeightedSortFunction.swift +++ b/Sources/Classes/Sort Functions/WeightedSortFunction.swift @@ -44,12 +44,12 @@ public struct WeightedSortFunction: PositionSortFunction, WeightSortFunction { self.position = position } - public func weights(forView view: UIView, recursiveDepth: Int) -> [WeightedView] { + public func weights(for view: UIView, recursiveDepth: Int) -> [WeightedView] { let subviews = view.spruce.subviews(withRecursiveDepth: recursiveDepth) let comparisonPoint = distancePoint(view: view, subviews: subviews) var maxWeight: Double = 0.0 - let weightedViews: [WeightedView] = subviews.map { + var weightedViews: [WeightedView] = subviews.map { let horizontalDistance = comparisonPoint.spruce.horizontalDistance(to: $0.referencePoint) * horizontalWeight.coefficient let verticalDistance = comparisonPoint.spruce.verticalDistance(to: $0.referencePoint) * verticalWeight.coefficient let distance = horizontalDistance + verticalDistance @@ -58,8 +58,8 @@ public struct WeightedSortFunction: PositionSortFunction, WeightSortFunction { } if reversed { - for var weightedView in weightedViews { - weightedView.weight = maxWeight - weightedView.weight + for index in 0.. Bool in + func printWeightedViews(_ weightedViews: [WeightedView]) { + let weightedViews = weightedViews.sorted { (left, right) -> Bool in guard let leftTag = left.spruceView.view?.tag, let rightTag = right.spruceView.view?.tag else { XCTFail("Failed to find UIView Object") @@ -93,12 +93,12 @@ class SortFunctionTests: XCTestCase { return leftTag < rightTag } print("[", terminator: "") - for index in 0..