Last active
May 17, 2019 17:51
-
-
Save PadraigK/45176ae13acab210e1387397c26f89db to your computer and use it in GitHub Desktop.
Demonstrates clamping a closed range instead of min(maxBound, max(minBound, value))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Instead of this... | |
CGFloat minConstant = -60.0f; | |
CGFloat maxConstant = self.actionViewDefaultBottomConstraintConstant; | |
CGFloat stuckConstant = maxConstant - distanceToBottom; | |
// :chefpoop: | |
actionViewBottomContraintConstant = fminf(maxConstant, fmaxf(minConstant, stuckConstant)); | |
*/ | |
extension ClosedRange { | |
public func clamp(_ value : Bound) -> Bound { | |
return self.lowerBound > value ? self.lowerBound | |
: self.upperBound < value ? self.upperBound | |
: value | |
} | |
} | |
let minYPos : CGFloat = -60.0 | |
let maxYPos = actionViewDefaultBottomConstraintConstant | |
let location = maxYPos - distanceToBottom | |
// :chefkiss: | |
bottomConstant = (minYPos ... maxYPos).clamp(location) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment