Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/Classes/Full View/FullViewAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
}
10 changes: 5 additions & 5 deletions Sources/Classes/Sort Functions/CorneredSortFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<weightedViews.count {
weightedViews[index].weight = maxWeight - weightedViews[index].weight
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Classes/Sort Functions/DefaultSortFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct DefaultSortFunction: SortFunction {

}

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)
var currentWeight: Double = 0.0
var weightedViews: [WeightedView] = []
Expand Down
2 changes: 1 addition & 1 deletion Sources/Classes/Sort Functions/InlineSortFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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..<weightedViews.count {
weightedViews[index].weight = maxWeight - weightedViews[index].weight
}
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/Classes/Sort Functions/Protocols/SortFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ extension WeightedView: Equatable, Comparable {
public protocol SortFunction {

/// 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.
/// - Note: This method simply calls the `weights(forView view: UIView, recursiveDepth: Int)` with a `recursiveDepth` of `0`.
/// - Note: This method simply calls the `weights(for view: UIView, recursiveDepth: Int)` with a `recursiveDepth` of `0`.
///
/// - Parameter view: the view whose subviews should be animated. This view should not be included in the returned array
/// - 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) -> [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.
///
/// - Parameters:
/// - 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)
}
}
2 changes: 1 addition & 1 deletion Sources/Classes/Sort Functions/RandomSortFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
8 changes: 4 additions & 4 deletions Sources/Classes/Sort Functions/WeightedSortFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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..<weightedViews.count {
weightedViews[index].weight = maxWeight - weightedViews[index].weight
}
}

Expand Down
8 changes: 4 additions & 4 deletions Spruce/Spruce.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
DBFA708F1E5CBC44000CF1DB /* DefaultSortFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBFA708E1E5CBC44000CF1DB /* DefaultSortFunctionTests.swift */; };
DBFA70911E5CBF10000CF1DB /* LinearSortFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBFA70901E5CBF10000CF1DB /* LinearSortFunctionTests.swift */; };
DBFA70931E5CC052000CF1DB /* InlineSortFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBFA70921E5CC052000CF1DB /* InlineSortFunctionTests.swift */; };
DBFA709A1E5DEBE5000CF1DB /* ContinuousWeightedSortFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBFA70991E5DEBE5000CF1DB /* ContinuousWeightedSortFunctionTests.swift */; };
DBFA709A1E5DEBE5000CF1DB /* WeightedSortFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBFA70991E5DEBE5000CF1DB /* WeightedSortFunctionTests.swift */; };
DBFAC1311E4A5DEE00F534D5 /* SortFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBFAC1301E4A5DEE00F534D5 /* SortFunctionTests.swift */; };
DBFAC1371E4A67E600F534D5 /* CorneredSortFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBFAC1361E4A67E600F534D5 /* CorneredSortFunctionTests.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -100,7 +100,7 @@
DBFA708E1E5CBC44000CF1DB /* DefaultSortFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DefaultSortFunctionTests.swift; sourceTree = "<group>"; };
DBFA70901E5CBF10000CF1DB /* LinearSortFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LinearSortFunctionTests.swift; sourceTree = "<group>"; };
DBFA70921E5CC052000CF1DB /* InlineSortFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InlineSortFunctionTests.swift; sourceTree = "<group>"; };
DBFA70991E5DEBE5000CF1DB /* ContinuousWeightedSortFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContinuousWeightedSortFunctionTests.swift; sourceTree = "<group>"; };
DBFA70991E5DEBE5000CF1DB /* WeightedSortFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WeightedSortFunctionTests.swift; sourceTree = "<group>"; };
DBFAC1301E4A5DEE00F534D5 /* SortFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SortFunctionTests.swift; sourceTree = "<group>"; };
DBFAC1361E4A67E600F534D5 /* CorneredSortFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CorneredSortFunctionTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -272,7 +272,7 @@
DBFA708E1E5CBC44000CF1DB /* DefaultSortFunctionTests.swift */,
DBFA70901E5CBF10000CF1DB /* LinearSortFunctionTests.swift */,
DBFA70921E5CC052000CF1DB /* InlineSortFunctionTests.swift */,
DBFA70991E5DEBE5000CF1DB /* ContinuousWeightedSortFunctionTests.swift */,
DBFA70991E5DEBE5000CF1DB /* WeightedSortFunctionTests.swift */,
DB7C757E1E7858BE0008C75C /* RandomSortFunctionTests.swift */,
);
path = "Sort Function Tests";
Expand Down Expand Up @@ -429,7 +429,7 @@
files = (
DB20E5771E4BADA9004C5A96 /* RadialSortFunctionTests.swift in Sources */,
DBFAC1371E4A67E600F534D5 /* CorneredSortFunctionTests.swift in Sources */,
DBFA709A1E5DEBE5000CF1DB /* ContinuousWeightedSortFunctionTests.swift in Sources */,
DBFA709A1E5DEBE5000CF1DB /* WeightedSortFunctionTests.swift in Sources */,
DBFA708F1E5CBC44000CF1DB /* DefaultSortFunctionTests.swift in Sources */,
DBFA70911E5CBF10000CF1DB /* LinearSortFunctionTests.swift in Sources */,
DBFA70931E5CC052000CF1DB /* InlineSortFunctionTests.swift in Sources */,
Expand Down
Loading