Skip to content
Merged
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
9 changes: 6 additions & 3 deletions TORoundedButton/TORoundedButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
@property (nonatomic, assign) IBInspectable CGFloat tappedTextAlpha;

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

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

/// The duration of the tapping cross-fade animation (Default is 0.4f).
@property (nonatomic, assign) CGFloat tapAnimationDuration;
/// The duration of the tap-down animation (Default is 0.1f).
@property (nonatomic, assign) CGFloat tapDownAnimationDuration;

/// The duration of the tap-up animation (Default is 0.4f).
@property (nonatomic, assign) CGFloat tapUpAnimationDuration;

/// A callback handler triggered each time the button is tapped.
@property (nonatomic, copy) void (^tappedHandler)(void);
Expand Down
76 changes: 65 additions & 11 deletions TORoundedButton/TORoundedButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ - (instancetype)initWithText:(NSString *)text {
- (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT {
// Default properties (Make sure they're not overriding IB)
_tappedTextAlpha = (_tappedTextAlpha > FLT_EPSILON) ?: 1.0f;
_tapAnimationDuration = (_tapAnimationDuration > FLT_EPSILON) ?: 0.4f;
_tapDownAnimationDuration = (_tapDownAnimationDuration > FLT_EPSILON) ?: 0.1f;
_tapUpAnimationDuration = (_tapUpAnimationDuration > FLT_EPSILON) ?: 0.4f;
_tappedButtonScale = (_tappedButtonScale > FLT_EPSILON) ?: 0.97f;
_tappedTintColorBrightnessOffset = !TORoundedButtonFloatIsZero(_tappedTintColorBrightnessOffset) ?: -0.15f;
_tappedTintColorBrightnessOffset = !TORoundedButtonFloatIsZero(_tappedTintColorBrightnessOffset) ?: 0.25f;
_contentInset = (UIEdgeInsets){15.0, 15.0, 15.0, 15.0};
_blurStyle = UIBlurEffectStyleDark;
_impactStyle = TORoundedButtonImpactStyleMedium;
Expand Down Expand Up @@ -257,7 +258,11 @@ - (void)tintColorDidChange {
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
[self setNeedsLayout];
[self _updateTappedTintColorForTintColor];
if (@available(iOS 13.0, *)) {
if ([previousTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:self.traitCollection]) {
[self _updateTappedTintColorForTintColor];
}
}
}

#pragma mark - View Layout -
Expand Down Expand Up @@ -385,7 +390,7 @@ - (void)_didDragInside {

- (void)_performTapAnimation:(void (^)(void))animations
completion:(void (^_Nullable)(BOOL finished))completion TOROUNDEDBUTTON_OBJC_DIRECT {
[UIView animateWithDuration:_tapAnimationDuration
[UIView animateWithDuration:_isTapped ? _tapDownAnimationDuration : _tapUpAnimationDuration
delay:0.0f
usingSpringWithDamping:1.0f
initialSpringVelocity:0.5f
Expand Down Expand Up @@ -656,7 +661,6 @@ - (void)_updateTappedTintColorForTintColor TOROUNDEDBUTTON_OBJC_DIRECT {
if (@available(iOS 13.0, *)) {
tintColor = [tintColor resolvedColorWithTraitCollection:self.traitCollection];
}

_tappedTintColor = [self _brightnessAdjustedColorWithColor:tintColor
amount:_tappedTintColorBrightnessOffset];
}
Expand All @@ -669,13 +673,63 @@ - (UIColor *)_labelBackgroundColor TOROUNDEDBUTTON_OBJC_DIRECT {
}

- (UIColor *)_brightnessAdjustedColorWithColor:(UIColor *)color amount:(CGFloat)amount TOROUNDEDBUTTON_OBJC_DIRECT {
if (!color) { return nil; }
// Clamp provided value just in case
amount = fmaxf(0.0, fminf(amount, 1.0));

CGFloat h = 0, s = 0, b = 0, a = 0;
if (![color getHue:&h saturation:&s brightness:&b alpha:&a]) { return color; }

// Zones:
// - RED - 0.0 / 1.0
// - GREEN - 0.33
// - BLUE - 0.66
BOOL isRed = (h < 0.05f || h > 0.95f);
BOOL isGreen = (h > 0.25f && h < 0.45f);
BOOL isBlue = (h > 0.55f && h < 0.75f);

CGFloat hueShift = 0.0f;
CGFloat satBoost = 0.0f;
CGFloat brightnessAdd = 0.0f;

if (isRed) {
// systemRed - nudge toward orange/yellow, slightly brighter, modest sat
hueShift = 0.08f * amount; // towards yellow
satBoost = 0.10f * amount;
brightnessAdd = 0.18f * amount;
}
else if (isGreen) {
// systemGreen - nudge toward yellow-green, clearly brighter + more vivid
hueShift = -0.08f * amount; // toward yellow-green
satBoost = 0.25f * amount; // push harder here
brightnessAdd = 0.22f * amount; // stronger lift so it’s clearly changed
}
else if (isBlue) {
// systemBlue - nudge toward cyan, bright + vivid
hueShift = -0.06f * amount; // toward cyan
satBoost = 0.18f * amount;
brightnessAdd = 0.18f * amount;
}
else {
// Everything else: mild generic brighten + hue wobble
hueShift = -0.03f * amount;
satBoost = 0.15f * amount;
brightnessAdd = 0.18f * amount;
}

// Apply hue shift with wrapping
CGFloat newH = h + hueShift;
if (newH < 0.0f) { newH += 1.0f; }
if (newH > 1.0f) { newH -= 1.0f; }

// Brighten: constant bump + small relative lift if not already maxed
CGFloat newB = b + brightnessAdd + (1.0f - b) * 0.2f * amount;
newB = fminf(newB, 1.0f);

// Saturation boost, clamped
CGFloat newS = s + satBoost;
newS = fminf(newS, 1.0f);

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

@end
2 changes: 1 addition & 1 deletion TORoundedButtonExampleTests/TORoundedButtonExampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (void)testDefaultValues
XCTAssertEqual(button.text, @"Test");
XCTAssertEqual(button.textColor, [UIColor whiteColor]);
XCTAssertEqual(button.tappedTextAlpha, 1.0f);
XCTAssertEqual(button.tappedTintColorBrightnessOffset, -0.15f);
XCTAssertEqual(button.tappedTintColorBrightnessOffset, 0.25f);
XCTAssertEqual(button.tappedButtonScale, 0.97f);

#ifdef __IPHONE_26_0
Expand Down