Last active
December 11, 2015 21:48
-
-
Save dphase/4665341 to your computer and use it in GitHub Desktop.
UIBezierPath Warped Shadow for RubyMotion, idea from http://nscookbook.com/2013/01/ios-programming-recipe-10-adding-a-shadow-to-uiview/
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
# ================================= | |
# = Warped shadow for any CGLayer = | |
# ================================= | |
def warpShadow(rect, offset) | |
size = rect.size | |
path = UIBezierPath.bezierPath | |
# Top/Right: ````| | |
path.moveToPoint([offset, offset]) | |
path.addLineToPoint([size.width - offset, offset]) | |
path.addLineToPoint([size.width - (offset * 0.75), size.height + offset]) | |
# Bottom/Left: |.-`-. | |
path.addQuadCurveToPoint([offset * 0.75, size.height + offset], | |
controlPoint:[size.width / 2.0, size.height - offset]) | |
path.closePath | |
path.CGPath | |
end | |
# --------------------------------------------------------------------------------------- | |
# Example: http://shell.reverse.net/~dphase/sshots/warpshadow.png | |
# Source: | |
box = UIView.alloc.initWithFrame([[30,30],[400,75]]).tap do |v| | |
v.backgroundColor = 0x6c8ea3.uicolor(0.8) | |
v.layer.shadowOffset = CGSizeZero # Set zero since offset is handled by warpShadow method | |
v.layer.shadowColor = :black.uicolor.cgcolor | |
v.layer.shadowOpacity = 0.5 | |
v.layer.shadowRadius = 1.25 | |
v.layer.shadowPath = warpShadow(v.layer.bounds, 5) # Params are current layer and offset | |
end | |
view << box |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example screenshot:

http://shell.reverse.net/~dphase/sshots/warpshadow.png