Skip to content

Commit

Permalink
[Tabs] Allow setting ripple color. (#7784)
Browse files Browse the repository at this point in the history
Adds a property that customizes the ripple color.

## Example View Controller

|Before|After|
|---|---|
|![Simulator Screen Shot - iPhone X - 2019-07-03 at 00 14 24](https://user-images.githubusercontent.com/1753199/60562949-a809a300-9d27-11e9-963d-830356ed845c.png)|![Simulator Screen Shot - iPhone X - 2019-07-03 at 00 12 49](https://user-images.githubusercontent.com/1753199/60562952-ad66ed80-9d27-11e9-8013-db5dba602bf3.png)|

Closes #7657
  • Loading branch information
Robert Moore committed Jul 3, 2019
1 parent 1065634 commit 88e5a52
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
Expand Up @@ -130,6 +130,8 @@ - (void)viewDidLoad {
forState:UIControlStateNormal];
[self.tabBar setTitleFont:[UIFont systemFontOfSize:16] forState:UIControlStateSelected];
self.tabBar.selectionIndicatorStrokeColor = self.containerScheme.colorScheme.onSecondaryColor;
self.tabBar.rippleColor =
[self.containerScheme.colorScheme.primaryColor colorWithAlphaComponent:(CGFloat)0.1];
self.tabBar.selectedItem = item4;
self.tabBar.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.tabBar];
Expand Down
5 changes: 5 additions & 0 deletions components/Tabs/src/TabBarView/MDCTabBarView.h
Expand Up @@ -34,6 +34,11 @@ __attribute__((objc_subclassing_restricted)) @interface MDCTabBarView : UIScroll
/** The color of the Tab bar's background. */
@property(nullable, nonatomic, copy) UIColor *barTintColor;

/**
The color for the Ripple effect for touch feedback.
*/
@property(nonnull, nonatomic, copy) UIColor *rippleColor;

/** The tab bar view delegate. */
@property(nullable, nonatomic, weak) id<MDCTabBarViewDelegate> tabBarDelegate;

Expand Down
18 changes: 18 additions & 0 deletions components/Tabs/src/TabBarView/MDCTabBarView.m
Expand Up @@ -70,6 +70,7 @@ @interface MDCTabBarView ()

/** The title font for bar items. */
@property(nonnull, nonatomic, strong) NSMutableDictionary<NSNumber *, UIFont *> *stateToTitleFont;

@end

@implementation MDCTabBarView
Expand All @@ -82,6 +83,7 @@ @implementation MDCTabBarView
- (instancetype)init {
self = [super init];
if (self) {
_rippleColor = [[UIColor alloc] initWithWhite:0 alpha:(CGFloat)0.16];
_needsScrollToSelectedItem = YES;
_items = @[];
_stateToImageTintColor = [NSMutableDictionary dictionary];
Expand Down Expand Up @@ -122,6 +124,21 @@ - (UIColor *)barTintColor {
return self.backgroundColor;
}

- (void)updateRippleColorForAllViews {
for (UIView *subview in self.itemViews) {
if (![subview isKindOfClass:[MDCTabBarViewItemView class]]) {
continue;
}
MDCTabBarViewItemView *itemView = (MDCTabBarViewItemView *)subview;
itemView.rippleTouchController.rippleView.rippleColor = self.rippleColor;
}
}

- (void)setRippleColor:(UIColor *)rippleColor {
_rippleColor = [rippleColor copy];
[self updateRippleColorForAllViews];
}

- (void)setSelectionIndicatorStrokeColor:(UIColor *)selectionIndicatorStrokeColor {
_selectionIndicatorStrokeColor = selectionIndicatorStrokeColor ?: UIColor.blackColor;
self.selectionIndicatorView.tintColor = self.selectionIndicatorStrokeColor;
Expand Down Expand Up @@ -162,6 +179,7 @@ - (void)setItems:(NSArray<UITabBarItem *> *)items {
: item.accessibilityTraits;
mdcItemView.titleLabel.textColor = [self titleColorForState:UIControlStateNormal];
mdcItemView.iconImageView.image = item.image;
mdcItemView.rippleTouchController.rippleView.rippleColor = self.rippleColor;
itemView = mdcItemView;
}
UITapGestureRecognizer *tapGesture =
Expand Down
Expand Up @@ -17,6 +17,7 @@
#import "MDCTabBarItem.h"
#import "MDCTabBarView.h"
#import "MDCTabBarViewIndicatorSupporting.h"
#import "MDCTabBarViewItemView.h"

/** The typical size of an image in a Tab bar. */
static const CGSize kTypicalImageSize = (CGSize){24, 24};
Expand Down Expand Up @@ -88,6 +89,11 @@ - (CGSize)sizeThatFits:(CGSize)size {
@"وتم عل والقرى إتفاقية, عن هذا وباءت الغالي وفرنسا.";
static NSString *const kItemTitleLong3Arabic = @"تحت أي قدما وإقامة. ودول بشرية اليابانية لان ما.";

/** Exposing some internal properties to aid in testing. */
@interface MDCTabBarView (SnapshotTesting)
@property(nonnull, nonatomic, copy) NSArray<UIView *> *itemViews;
@end

/** A test class that allows setting safe area insets. */
@interface MDCTabBarViewSnapshotTestsSuperview : UIView
/** Allows overriding the safe area insets. */
Expand Down Expand Up @@ -182,6 +188,24 @@ - (void)tearDown {

#pragma mark - Helpers

- (void)activateRippleInView:(MDCTabBarView *)tabBarView forItem:(UITabBarItem *)item {
NSUInteger indexOfItem = [tabBarView.items indexOfObject:item];
if (indexOfItem == NSNotFound || indexOfItem >= tabBarView.itemViews.count) {
NSAssert(NO, @"(%@) has no associated item view.", item);
return;
}
UIView *itemView = tabBarView.itemViews[indexOfItem];
if (![itemView isKindOfClass:[MDCTabBarViewItemView class]]) {
return;
}
MDCTabBarViewItemView *mdcItemView = (MDCTabBarViewItemView *)itemView;
[mdcItemView.rippleTouchController.rippleView
beginRippleTouchDownAtPoint:CGPointMake(CGRectGetMidX(mdcItemView.bounds),
CGRectGetMidY(mdcItemView.bounds))
animated:NO
completion:nil];
}

- (void)changeToLatinStringsWithLongTitles:(BOOL)useLongTitles {
if (useLongTitles) {
self.item1.title = kItemTitleLong1Latin;
Expand Down Expand Up @@ -952,6 +976,23 @@ - (void)testBarTintColor {
[self generateSnapshotAndVerifyForView:self.tabBarView];
}

- (void)testRippleColor {
// Given
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"One" image:self.typicalIcon1 tag:0];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"Two" image:self.typicalIcon2 tag:2];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Three" image:self.typicalIcon3 tag:3];
self.tabBarView.items = @[ item1, item2, item3 ];
[self.tabBarView setSelectedItem:item2 animated:NO];
[self.tabBarView sizeToFit];

// When
self.tabBarView.rippleColor = [UIColor.orangeColor colorWithAlphaComponent:(CGFloat)0.25];
[self activateRippleInView:self.tabBarView forItem:item1];

// Then
[self generateSnapshotAndVerifyForView:self.tabBarView];
}

- (void)testCustomSelectionIndicatorStrokeColor {
// Given
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"One" image:self.typicalIcon1 tag:0];
Expand Down
6 changes: 6 additions & 0 deletions components/Tabs/tests/unit/TabBarView/MDCTabBarViewTests.m
Expand Up @@ -273,6 +273,12 @@ - (void)testSettingBackgroundColorUpdatesBarTintColor {
XCTAssertEqual(self.tabBarView.barTintColor, self.tabBarView.backgroundColor);
}

- (void)testDefaultRippleColor {
// Then
XCTAssertEqualObjects(self.tabBarView.rippleColor, [[UIColor alloc] initWithWhite:0
alpha:(CGFloat)0.16]);
}

- (void)testImageTintColorForStateFallsBackToNormalState {
// Given
[self.tabBarView setImageTintColor:nil forState:UIControlStateNormal];
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 88e5a52

Please sign in to comment.