Skip to content

Commit

Permalink
[Ripple] Add a convenience API for finding the ripple view in the vie…
Browse files Browse the repository at this point in the history
…w's subviews.

If a ripple view is not found, a new instance of a ripple view is created.

PiperOrigin-RevId: 317610319
  • Loading branch information
yarneo authored and material-automation committed Jun 22, 2020
1 parent 9c99bc9 commit 5462d27
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
15 changes: 15 additions & 0 deletions components/Ripple/src/MDCRippleView.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,19 @@ typedef NS_ENUM(NSInteger, MDCRippleStyle) {
- (void)beginRippleTouchUpAnimated:(BOOL)animated
completion:(nullable MDCRippleCompletionBlock)completion;

/**
Enumerates the given view's subviews for an instance of MDCRippleView and returns it if found, or
creates and adds a new instance of MDCRippleView if not.
This method is a convenience method for adding ripple to an arbitrary view without needing to
subclass the target view. Use this method in situations where you expect there to be many distinct
ripple views in existence for a single ripple touch controller. Example scenarios include:
- Adding ripple to individual collection view/table view cells
This method can be used in your MDCRippleTouchController delegate's
-rippleTouchController:rippleViewAtTouchLocation: implementation.
*/
+ (nonnull MDCRippleView *)injectedRippleViewForView:(nonnull UIView *)view;

@end
16 changes: 16 additions & 0 deletions components/Ripple/src/MDCRippleView.m
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ - (void)rippleLayerTouchUpAnimationDidEnd:(MDCRippleLayer *)rippleLayer {
return nil;
}

#pragma mark - Convenience API

+ (MDCRippleView *)injectedRippleViewForView:(UIView *)view {
for (MDCRippleView *subview in view.subviews) {
if ([subview isKindOfClass:[MDCRippleView class]]) {
return subview;
}
}

MDCRippleView *newRippleView = [[MDCRippleView alloc] initWithFrame:view.bounds];
newRippleView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[view addSubview:newRippleView];
return newRippleView;
}

@end

@implementation MDCRipplePendingAnimation
Expand Down
27 changes: 27 additions & 0 deletions components/Ripple/tests/unit/MDCRippleViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,31 @@ - (void)testTraitCollectionDidChangeBlockCalledWithExpectedParameters {
XCTAssertEqual(passedTraitCollection, fakeTraitCollection);
}

- (void)testInjectedRippleViewFindsRippleViewInHierarchy {
// Given
UIView *parentView = [[UIView alloc] init];
MDCRippleView *rippleView = [[MDCRippleView alloc] init];
[parentView addSubview:rippleView];

// When
MDCRippleView *injectedRippleView = [MDCRippleView injectedRippleViewForView:parentView];

// Then
XCTAssertEqual(rippleView, injectedRippleView);
}

- (void)testInjectedRippleViewCreatesRippleViewInstance {
// Given
UIView *firstLevelView = [[UIView alloc] init];
UIView *secondLevelView = [[UIView alloc] init];
[firstLevelView addSubview:secondLevelView];

// When
MDCRippleView *injectedRippleView = [MDCRippleView injectedRippleViewForView:firstLevelView];

// Then
XCTAssertNotNil(injectedRippleView);
XCTAssertEqualObjects(injectedRippleView.superview, firstLevelView);
}

@end

0 comments on commit 5462d27

Please sign in to comment.