Skip to content

Instantly share code, notes, and snippets.

@dphase
Last active December 11, 2015 21:48
Show Gist options
  • Save dphase/4665341 to your computer and use it in GitHub Desktop.
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/
# =================================
# = 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
@dphase
Copy link
Author

dphase commented Jan 29, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment