Last active
August 5, 2016 08:14
-
-
Save mosluce/f436f6c71ad4dec86befb2136fcddd91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ShadowViewB.swift | |
// SwiftShadow | |
// | |
// Created by 默司 on 2016/8/4. | |
// Copyright © 2016年 默司. All rights reserved. | |
// | |
import UIKit | |
@IBDesignable | |
class ExampleView: UIView { | |
let innerView = UIView() | |
var cornerRadius: CGFloat = 10 { | |
didSet { | |
self.layer.cornerRadius = cornerRadius | |
self.drawMaterialShadow() | |
} | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
setup() | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setup() | |
} | |
func setup() { | |
self.layer.cornerRadius = cornerRadius | |
self.drawMaterialShadow() | |
self.innerView.frame = CGRectMake(0, 0, self.bounds.width - 32, self.bounds.height - 32) | |
self.innerView.backgroundColor = UIColor.whiteColor() | |
self.addSubview(innerView) | |
self.innerView.center = CGPointMake(self.bounds.width/2, self.bounds.height/2) | |
self.innerView.drawMaterialShadow() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+MaterialShadow.swift | |
// SwiftShadow | |
// | |
// Created by 默司 on 2016/8/5. | |
// Copyright © 2016年 默司. All rights reserved. | |
// | |
import UIKit | |
extension UIView { | |
func drawMaterialShadow() { | |
drawMaterialShadow(1) | |
} | |
func drawMaterialShadow(depth: UInt) { | |
var depth = depth | |
if depth < 1 { | |
depth = 1 | |
} | |
if depth > 5 { | |
depth = 5 | |
} | |
let shadowLayer = CAShapeLayer() | |
let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius) | |
let topShadowLayer = CAShapeLayer() | |
topShadowLayer.shadowPath = path.CGPath | |
topShadowLayer.shadowOpacity = 1.0 | |
let bottomShadowLayer = CAShapeLayer() | |
bottomShadowLayer.shadowPath = path.CGPath | |
bottomShadowLayer.shadowOpacity = 1.0 | |
switch depth { | |
case 1: | |
topShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.12 ).CGColor | |
topShadowLayer.shadowRadius = 1.5 | |
topShadowLayer.shadowOffset = CGSizeMake(0, 1.0) | |
bottomShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.24 ).CGColor | |
bottomShadowLayer.shadowRadius = 1.0 | |
bottomShadowLayer.shadowOffset = CGSizeMake(0, 1.0) | |
break | |
case 2: | |
topShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.16 ).CGColor | |
topShadowLayer.shadowRadius = 3.0 | |
topShadowLayer.shadowOffset = CGSizeMake(0, 3.0) | |
bottomShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.23 ).CGColor | |
bottomShadowLayer.shadowRadius = 3.0 | |
bottomShadowLayer.shadowOffset = CGSizeMake(0, 3.0) | |
break | |
case 3: | |
topShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.19 ).CGColor | |
topShadowLayer.shadowRadius = 10.0 | |
topShadowLayer.shadowOffset = CGSizeMake(0, 10.0) | |
bottomShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.23 ).CGColor | |
bottomShadowLayer.shadowRadius = 6.0 | |
bottomShadowLayer.shadowOffset = CGSizeMake(0, 3.0) | |
break | |
case 4: | |
topShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.25 ).CGColor | |
topShadowLayer.shadowRadius = 14.0 | |
topShadowLayer.shadowOffset = CGSizeMake(0, 14.0) | |
bottomShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.22 ).CGColor | |
bottomShadowLayer.shadowRadius = 10.0 | |
bottomShadowLayer.shadowOffset = CGSizeMake(0, 5.0) | |
break | |
case 5: | |
topShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.30 ).CGColor | |
topShadowLayer.shadowRadius = 19.0 | |
topShadowLayer.shadowOffset = CGSizeMake(0, 19.0) | |
bottomShadowLayer.shadowColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.22 ).CGColor | |
bottomShadowLayer.shadowRadius = 15.0 | |
bottomShadowLayer.shadowOffset = CGSizeMake(0, 6.0) | |
break | |
default: | |
break | |
} | |
shadowLayer.addSublayer(bottomShadowLayer) | |
shadowLayer.addSublayer(topShadowLayer) | |
let layer = CALayer() | |
layer.frame = self.bounds | |
layer.cornerRadius = self.layer.cornerRadius | |
layer.backgroundColor = self.backgroundColor?.CGColor | |
self.layer.insertSublayer(layer, atIndex: 0) | |
self.layer.insertSublayer(shadowLayer, atIndex: 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment