Will Larche 1080e89422 [Documentation] Putting Swift snippets before Objc (#960)
* [ActivityIndicator] Swift first in README

* [AnimationTiming] Swift first in readme.

* [AppBar] Swift first in readme.

* [ButtonBar] Swift first in readme.

* [Buttons] Swift first in readme.

* [CollectionLayoutAttributes] Swift first in readme

* [Collections] Swift first in readme.

* [Dialogs] Swift first in readme.

* [FeatureHighlight] Swift first in readme.

* [FlexibleHeader] Swift first in readme.

* [FontDiskLoader] Swift first in readme.

* [HeaderStackView] Swift first in readme.

* [Ink] Swift first in readme.

* [NavigationBar] Swift first in readme.

* [OverlayWindow] Adding missing site comments. Swift first in readme.

* [PageControl] Swift first in readme.

* [Palettes] Swift first in readme.

* [ProgressView] Swift first in readme.

* [RobotoFontLoader] Swift first in readme.

* [ShadowElevations] Swift first in readme.

* [ShadowLayer] Swift first in readme.

* [Slider] Swift first in readme.

* [Snackbar] Swift first in readme.

* [SpritedAnimationView] Swift first in readme.

* [Switch] Swift first in readme.

* [Typography] Swift first in readme.

* [ShadowLayer] Reducing font size in readme.

* [Switch] Reducing font size in readme.
2016-12-07 10:31:34 -05:00
..
2016-12-05 14:52:46 -05:00

Shadow Layer

Shadow Layer

Shadow Layer implements the material design specifications for elevation and shadows. By simulating the physical properties of paper, elevation and light source, shadows give visual depth to components. Shadow Layer provides an elevation property which affects a shadow's depth and strength, automatically handling shadow diffusion based on the shadow's elevation.

MDCShadowLayer

MDCShadowLayer provides a Core Animation CALayer that will render a shadow based on its elevation property. UIViews can utilize this by overriding their layerClass method to return MDCShadowLayer.

elevation sets the diffusion level of the shadow. The higher the shadow elevation, the more diffused the shadow becomes. Elevation uses points as a unit to specify height. Common shadow elevations are defined in MDCShadowElevations and range from 0 to 24 points. The shadow diffusion effect diminishes as elevations exceed 24 points.

Set shadowMaskEnabled to ensure the interior, non-shadow portion of the layer is visible. This is enabled by default and the internal portion of the layer is cut out.

MDCShadowMetrics

MDCShadowMetrics is a series of properties used to set MDCShadowLayer. MDCShadowLayer consists of two distinct layers. The overlay of these two layers generates a single material design shadow that adheres to defined height and light source principles.

Design Specifications

API Documentation


Installation

Requirements

  • Xcode 7.0 or higher.
  • iOS SDK version 7.0 or higher.

Installation with CocoaPods

To add this component to your Xcode project using CocoaPods, add the following to your Podfile:

pod 'MaterialComponents/ShadowLayer'

Then, run the following command:

pod install

Usage

Importing

Before using Shadow Layer, you'll need to import it:

Swift

import MaterialComponents

Objective-C

#import "MaterialShadowLayer.h"

Example of a custom button based on UIButton with material design shadows:

Swift

class ShadowButton: UIButton {

  override class func layerClass() -> AnyClass {
    return MDCShadowLayer.self
  }

}

Objective C

@interface ShadowButton : UIButton

@end

@implementation ShadowButton

+ (Class)layerClass {
  return [MDCShadowLayer class];
}

@end

Add the custom button to view:

Swift

let button: ShadowButton = ShadowButton.init(type: UIButtonType.System)
button.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
button.setTitle("Button", forState: UIControlState.Normal)
(button.layer as! MDCShadowLayer).setElevation(6.0)
self.addSubview(button)

Objective C

ShadowButton *button = [ShadowButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 200, 50);
[button setTitle: @"Button" forState:UIControlStateNormal];
[(MDCShadowLayer *)button.layer setElevation:6.f];
[self.view addSubview:button];

Creating a custom UIView with a shadow:

Swift

class ShadowedView: UIView {

  override class func layerClass() -> AnyClass {
    return MDCShadowLayer.self
  }

  var shadowLayer: MDCShadowLayer {
    return self.layer as! MDCShadowLayer
  }

  func setElevation(points: CGFloat) {
    self.shadowLayer.elevation = points
  }

}

Objective C

@interface ShadowedView : UIView
@end

@implementation ShadowedView

+ (Class)layerClass {
  return [MDCShadowLayer class];
}

- (MDCShadowLayer)shadowLayer {
  return (MDCShadowLayer *)self.layer;
}

- (void)setElevation:(CGFloat)points {
  [(MDCShadowLayer *)self.layer setElevation:points];
}

@end

To improve performance, consider rasterizing MDCShadowLayer when the view using the shadow is not animating or changing size.

Swift


self.layer.shouldRasterize = true;
self.layer.rasterizationScale = UIScreen.mainScreen().scale

Objective C


self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;

Disable rasterization before animating MDCShadowLayer.