|
| 1 | +# Shadow |
| 2 | + |
| 3 | +Provides lightweight classes that hold shadow properties that each correspond to an elevation. |
| 4 | + |
| 5 | +This library provides two immutable types that are created via builder APIs: |
| 6 | +MDCShadowsCollection and MDCShadow. This library works with standard CALayer shadow APIs in order |
| 7 | +to optimize for performance. UIView’s layer shadow properties are set directly, including: |
| 8 | +shadowOffset, shadowRadius, shadowOpacity, shadowColor, and shadowPath. We ensure that |
| 9 | +shadowPath is consistently set alongside the other properties in order for shadows to benefit |
| 10 | +from the performance gains that shadowPath provides. |
| 11 | + |
| 12 | +This library is part of an effort to simplify shadows in MDC, with the intent of deleting the |
| 13 | +MDCShadowLayer component entirely. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +We want to provide a simple API to integrate this shadow solution. Therefore, we leaned on providing only one type of configuration method that should be re-called in the view’s lifecycle. Namely, the method should be the C function MDCConfigureShadowForView. |
| 18 | + |
| 19 | +MDCConfigureShadowForView should be called in the start of the view’s lifecycle, i.e. during initialization, so that the shadowRadius, shadowOffset, shadowColor, and shadowOpacity are set on the layer. |
| 20 | + |
| 21 | +MDCConfigureShadowForView should also be called when the elevation is changed for the view i.e. it is set by the client, or there is a state change that also changes the elevation. This is to ensure that the right shadow values for that elevation are set on the layer. |
| 22 | +Lastly, MDCConfigureShadowForView should be called in layoutSubviews, to ensure that the shadowPath is set using the most upto date bounds and cornerRadius of the view. |
| 23 | + |
| 24 | +### Importing |
| 25 | + |
| 26 | +Before using Shadow, you'll need to import it: |
| 27 | + |
| 28 | +#### Swift |
| 29 | +```swift |
| 30 | +import MaterialComponents |
| 31 | + |
| 32 | +var shadowsCollection: MDCShadowsCollection |
| 33 | +let shadow = MDCShadowBuilder.builder(withOpacity:0.1, radius: 0.2, offset: CGSize(0.3, 0.4)).build() |
| 34 | +let shadowsBuilder = MDCShadowsCollectionBuilder.builder(withShadow:shadow forElevation:0) |
| 35 | +let shadowValuesForElevation = [1: MDCShadowBuilder.builder(withOpacity:0.3, |
| 36 | + radius: 0.4, |
| 37 | + offset: CGSize(0.5, 0.6)).build()] |
| 38 | +shadowsBuilder.addShadows(forElevation:shadowValuesForElevation) |
| 39 | +shadowsCollection = shadowsBuilder.build() |
| 40 | + |
| 41 | +... |
| 42 | + |
| 43 | +func updateShadow() { |
| 44 | + MDCConfigureShadowForView(self, |
| 45 | + self.shadowsCollection.shadow(forElevation:self.mdc_currentElevation), |
| 46 | + self.shadowColor) |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +#### Objective-C |
| 51 | +```objc |
| 52 | +#import "MaterialShadow.h" |
| 53 | + |
| 54 | +MDCShadowsCollection *shadowsCollection; |
| 55 | +MDCShadow *shadow = |
| 56 | + [[MDCShadowBuilder builderWithOpacity:0.1 |
| 57 | + radius:0.2 |
| 58 | + offset:CGSizeMake(0.3, 0.4)] build]; |
| 59 | +MDCShadowsCollectionBuilder *shadowsBuilder = |
| 60 | + [MDCShadowsCollectionBuilder builderWithShadow:shadow forElevation:0]; |
| 61 | +NSDictionary<NSNumber *, MDCShadow *> *shadowValuesForElevation = @{ |
| 62 | + @1 : [[MDCShadowBuilder builderWithOpacity:0.3 |
| 63 | + radius:0.4 |
| 64 | + offset:CGSizeMake(0.5, 0.6)] build], |
| 65 | +}; |
| 66 | +[shadowsBuilder addShadowsForElevations:shadowValuesForElevation]; |
| 67 | +shadowsCollection = [shadowsBuilder build]; |
| 68 | + |
| 69 | +... |
| 70 | + |
| 71 | +- (void)updateShadow { |
| 72 | + MDCConfigureShadowForView(self, |
| 73 | + [self.shadowsCollection shadowForElevation:self.mdc_currentElevation], |
| 74 | + self.shadowColor); |
| 75 | +} |
| 76 | +``` |
0 commit comments