Skip to content

Commit e81816d

Browse files
authored
Merge pull request #58 from TimOliver/colors
Refine the tapped color value
2 parents e614d5d + 6faad92 commit e81816d

3 files changed

Lines changed: 72 additions & 15 deletions

File tree

TORoundedButton/TORoundedButton.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
116116
@property (nonatomic, assign) IBInspectable CGFloat tappedTextAlpha;
117117

118118
/// Taking the default button background color apply a brightness offset for the tapped color
119-
/// (Default is -0.1f. Set 0.0 for off).
119+
/// (Default is 0.25f. Set 0.0 for off).
120120
@property (nonatomic, assign) IBInspectable CGFloat tappedTintColorBrightnessOffset;
121121

122122
/// If desired, explicity set the background color of the button when tapped (Default is nil).
@@ -125,8 +125,11 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
125125
/// When tapped, the scale by which the button shrinks during the animation (Default is 0.97f).
126126
@property (nonatomic, assign) IBInspectable CGFloat tappedButtonScale;
127127

128-
/// The duration of the tapping cross-fade animation (Default is 0.4f).
129-
@property (nonatomic, assign) CGFloat tapAnimationDuration;
128+
/// The duration of the tap-down animation (Default is 0.1f).
129+
@property (nonatomic, assign) CGFloat tapDownAnimationDuration;
130+
131+
/// The duration of the tap-up animation (Default is 0.4f).
132+
@property (nonatomic, assign) CGFloat tapUpAnimationDuration;
130133

131134
/// A callback handler triggered each time the button is tapped.
132135
@property (nonatomic, copy) void (^tappedHandler)(void);

TORoundedButton/TORoundedButton.m

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ - (instancetype)initWithText:(NSString *)text {
116116
- (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT {
117117
// Default properties (Make sure they're not overriding IB)
118118
_tappedTextAlpha = (_tappedTextAlpha > FLT_EPSILON) ?: 1.0f;
119-
_tapAnimationDuration = (_tapAnimationDuration > FLT_EPSILON) ?: 0.4f;
119+
_tapDownAnimationDuration = (_tapDownAnimationDuration > FLT_EPSILON) ?: 0.1f;
120+
_tapUpAnimationDuration = (_tapUpAnimationDuration > FLT_EPSILON) ?: 0.4f;
120121
_tappedButtonScale = (_tappedButtonScale > FLT_EPSILON) ?: 0.97f;
121-
_tappedTintColorBrightnessOffset = !TORoundedButtonFloatIsZero(_tappedTintColorBrightnessOffset) ?: -0.15f;
122+
_tappedTintColorBrightnessOffset = !TORoundedButtonFloatIsZero(_tappedTintColorBrightnessOffset) ?: 0.25f;
122123
_contentInset = (UIEdgeInsets){15.0, 15.0, 15.0, 15.0};
123124
_blurStyle = UIBlurEffectStyleDark;
124125
_impactStyle = TORoundedButtonImpactStyleMedium;
@@ -257,7 +258,11 @@ - (void)tintColorDidChange {
257258
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
258259
[super traitCollectionDidChange:previousTraitCollection];
259260
[self setNeedsLayout];
260-
[self _updateTappedTintColorForTintColor];
261+
if (@available(iOS 13.0, *)) {
262+
if ([previousTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:self.traitCollection]) {
263+
[self _updateTappedTintColorForTintColor];
264+
}
265+
}
261266
}
262267

263268
#pragma mark - View Layout -
@@ -385,7 +390,7 @@ - (void)_didDragInside {
385390

386391
- (void)_performTapAnimation:(void (^)(void))animations
387392
completion:(void (^_Nullable)(BOOL finished))completion TOROUNDEDBUTTON_OBJC_DIRECT {
388-
[UIView animateWithDuration:_tapAnimationDuration
393+
[UIView animateWithDuration:_isTapped ? _tapDownAnimationDuration : _tapUpAnimationDuration
389394
delay:0.0f
390395
usingSpringWithDamping:1.0f
391396
initialSpringVelocity:0.5f
@@ -656,7 +661,6 @@ - (void)_updateTappedTintColorForTintColor TOROUNDEDBUTTON_OBJC_DIRECT {
656661
if (@available(iOS 13.0, *)) {
657662
tintColor = [tintColor resolvedColorWithTraitCollection:self.traitCollection];
658663
}
659-
660664
_tappedTintColor = [self _brightnessAdjustedColorWithColor:tintColor
661665
amount:_tappedTintColorBrightnessOffset];
662666
}
@@ -669,13 +673,63 @@ - (UIColor *)_labelBackgroundColor TOROUNDEDBUTTON_OBJC_DIRECT {
669673
}
670674

671675
- (UIColor *)_brightnessAdjustedColorWithColor:(UIColor *)color amount:(CGFloat)amount TOROUNDEDBUTTON_OBJC_DIRECT {
672-
if (!color) { return nil; }
676+
// Clamp provided value just in case
677+
amount = fmaxf(0.0, fminf(amount, 1.0));
678+
679+
CGFloat h = 0, s = 0, b = 0, a = 0;
680+
if (![color getHue:&h saturation:&s brightness:&b alpha:&a]) { return color; }
681+
682+
// Zones:
683+
// - RED - 0.0 / 1.0
684+
// - GREEN - 0.33
685+
// - BLUE - 0.66
686+
BOOL isRed = (h < 0.05f || h > 0.95f);
687+
BOOL isGreen = (h > 0.25f && h < 0.45f);
688+
BOOL isBlue = (h > 0.55f && h < 0.75f);
689+
690+
CGFloat hueShift = 0.0f;
691+
CGFloat satBoost = 0.0f;
692+
CGFloat brightnessAdd = 0.0f;
693+
694+
if (isRed) {
695+
// systemRed - nudge toward orange/yellow, slightly brighter, modest sat
696+
hueShift = 0.08f * amount; // towards yellow
697+
satBoost = 0.10f * amount;
698+
brightnessAdd = 0.18f * amount;
699+
}
700+
else if (isGreen) {
701+
// systemGreen - nudge toward yellow-green, clearly brighter + more vivid
702+
hueShift = -0.08f * amount; // toward yellow-green
703+
satBoost = 0.25f * amount; // push harder here
704+
brightnessAdd = 0.22f * amount; // stronger lift so it’s clearly changed
705+
}
706+
else if (isBlue) {
707+
// systemBlue - nudge toward cyan, bright + vivid
708+
hueShift = -0.06f * amount; // toward cyan
709+
satBoost = 0.18f * amount;
710+
brightnessAdd = 0.18f * amount;
711+
}
712+
else {
713+
// Everything else: mild generic brighten + hue wobble
714+
hueShift = -0.03f * amount;
715+
satBoost = 0.15f * amount;
716+
brightnessAdd = 0.18f * amount;
717+
}
718+
719+
// Apply hue shift with wrapping
720+
CGFloat newH = h + hueShift;
721+
if (newH < 0.0f) { newH += 1.0f; }
722+
if (newH > 1.0f) { newH -= 1.0f; }
723+
724+
// Brighten: constant bump + small relative lift if not already maxed
725+
CGFloat newB = b + brightnessAdd + (1.0f - b) * 0.2f * amount;
726+
newB = fminf(newB, 1.0f);
727+
728+
// Saturation boost, clamped
729+
CGFloat newS = s + satBoost;
730+
newS = fminf(newS, 1.0f);
673731

674-
CGFloat h, s, b, a;
675-
if (![color getHue:&h saturation:&s brightness:&b alpha:&a]) { return nil; }
676-
b += amount;
677-
b = MAX(b, 0.0f); b = MIN(b, 1.0f);
678-
return [UIColor colorWithHue:h saturation:s brightness:b alpha:a];
732+
return [UIColor colorWithHue:newH saturation:newS brightness:newB alpha:a];
679733
}
680734

681735
@end

TORoundedButtonExampleTests/TORoundedButtonExampleTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ - (void)testDefaultValues
2222
XCTAssertEqual(button.text, @"Test");
2323
XCTAssertEqual(button.textColor, [UIColor whiteColor]);
2424
XCTAssertEqual(button.tappedTextAlpha, 1.0f);
25-
XCTAssertEqual(button.tappedTintColorBrightnessOffset, -0.15f);
25+
XCTAssertEqual(button.tappedTintColorBrightnessOffset, 0.25f);
2626
XCTAssertEqual(button.tappedButtonScale, 0.97f);
2727

2828
#ifdef __IPHONE_26_0

0 commit comments

Comments
 (0)