DistanceSortFunction

public protocol DistanceSortFunction: SortFunction

A SortFunction implementation that contain basic methods needed for doing distance comparisons. If you are implementing a SortFunction that considers distance to be one of it’s weighting mechanisms for sorting the views on the screen, then implementing this protocol would provide improvements over the standard SortFunction.

  • the time interval of delay between each of the objects on the screen when animating

    Declaration

    Swift

    var interObjectDelay: TimeInterval
  • a bool value as to whether the animation should proceeed forwards or backwards

    Declaration

    Swift

    var reversed: Bool
  • distanceBetween(_:and:) Default implementation

    Compute the distance between two points. This can be any distance metric as long as you keep it consistent. For the default implementation, euclideanDistance is used.

    Default Implementation

    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 time offets would be smaller near the center of the view and grow as they get further from the center point. - Note: With a DistanceSortFunction a comparison point is initially found that helps compute the offsets for each of the views. This comparison point is found by calling the method distancePoint on self. From there the default implementation is that the further the distance from the comparisonPoint to the referencePoint of each view the longer the offset. Since this is setup with a interObjectDelay every view with the same distance will have the same delay and those with different distances will have an incrementally large delay. For example, consider the distances [1,2,2,3,4]. If we were to analyze those distances with an interObjectDelay of .1 seconds, then the delays would look like[0, 0.1, 0.1, 0.2, 0.3]`. Notice that those views with the same distance have the same delay. - Note: A floor value is taken of the distances when comparing so that floating point operations don’t mess up the joining of like distances. Considering that distances are in terms of points on the screen, there was not need to consider half points in reality.

    Declaration

    Swift

    func distanceBetween(_ left: CGPoint, and right: CGPoint) -> Double

    Parameters

    left

    the left point

    right

    the right point

    Return Value

    the distance between the two points.

  • With a DistanceSortFunction there has to be some main point that all the views are compared to. Since we understand that this point changes based on some value, you need to implement this method that will return a point for the given SortFunction configuration. The timeOffsets method will use this to grab a comparison point. From there the further the distance from the point the longer the delay for that view to start animating.

    Declaration

    Swift

    func distancePoint(view: UIView, subviews: [View]) -> CGPoint

    Parameters

    view

    the view for which the point coordinates will base their values off of

    subviews

    the subviews that are allocated for the SortFunction. The reason these are passed into the function is so that you can actually grab a reference point of a subview that is closest to the comparison point. This is done so that at least one view will start at the 0s marker.

    Return Value

    a CGPoint object that will allow all views to compare their reference point to this returned value. If translate was used, this returned value should be equal to one of the subviews reference points.

  • Given a point, find the closest subview to that point and then return the reference point of that subview. This way there will be at least one distance calculation that will have a zero value. We want to make sure that the animation will start right when called, thus there needs to be one view with a zero distance.

    Default Implementation

    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 time offets would be smaller near the center of the view and grow as they get further from the center point. - Note: With a DistanceSortFunction a comparison point is initially found that helps compute the offsets for each of the views. This comparison point is found by calling the method distancePoint on self. From there the default implementation is that the further the distance from the comparisonPoint to the referencePoint of each view the longer the offset. Since this is setup with a interObjectDelay every view with the same distance will have the same delay and those with different distances will have an incrementally large delay. For example, consider the distances [1,2,2,3,4]. If we were to analyze those distances with an interObjectDelay of .1 seconds, then the delays would look like[0, 0.1, 0.1, 0.2, 0.3]`. Notice that those views with the same distance have the same delay. - Note: A floor value is taken of the distances when comparing so that floating point operations don’t mess up the joining of like distances. Considering that distances are in terms of points on the screen, there was not need to consider half points in reality.

    Declaration

    Swift

    func translate(distancePoint: CGPoint, intoSubviews subviews: [View]) -> CGPoint

    Parameters

    distancePoint

    the point on the main view

    subviews

    the subviews of the main view

    Return Value

    the reference point of the subview that is closest to the distancePoint

  • 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 time offets would be smaller near the center of the view and grow as they get further from the center point. - Note: With a DistanceSortFunction a comparison point is initially found that helps compute the offsets for each of the views. This comparison point is found by calling the method distancePoint on self. From there the default implementation is that the further the distance from the comparisonPoint to the referencePoint of each view the longer the offset. Since this is setup with a interObjectDelay every view with the same distance will have the same delay and those with different distances will have an incrementally large delay. For example, consider the distances [1,2,2,3,4]. If we were to analyze those distances with an interObjectDelay of .1 seconds, then the delays would look like[0, 0.1, 0.1, 0.2, 0.3]`. Notice that those views with the same distance have the same delay. - Note: A floor value is taken of the distances when comparing so that floating point operations don’t mess up the joining of like distances. Considering that distances are in terms of points on the screen, there was not need to consider half points in reality.

    Declaration

    Swift

    func timeOffsets(view: UIView, recursiveDepth: Int) -> [TimedView]

    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…

    Return Value

    an array of TimedView’s which contain references to the view needed to be animated and the time offset for when the animation of that individual view should start relative to the start of the overall animation