i looked couldn't find how can add inner shadow uiview, top (from top bottom) swift. best way add inner circle in swift?
edit: i've found questions & answers on either in obj-c or looks complicated. looking more swifty way, if there any
what want achieve:
here's pure swift version whipped up:
public class edgeshadowlayer: cagradientlayer { public enum edge { case top case left case bottom case right } public init(forview view: uiview, edge: edge = edge.top, shadowradius radius: cgfloat = 20.0, tocolor: uicolor = uicolor.white, fromcolor: uicolor = uicolor.black) { super.init() self.colors = [fromcolor.cgcolor, tocolor.cgcolor] self.shadowradius = radius let viewframe = view.frame switch edge { case .top: startpoint = cgpoint(x: 0.5, y: 0.0) endpoint = cgpoint(x: 0.5, y: 1.0) self.frame = cgrect(x: 0.0, y: 0.0, width: viewframe.width, height: shadowradius) case .bottom: startpoint = cgpoint(x: 0.5, y: 1.0) endpoint = cgpoint(x: 0.5, y: 0.0) self.frame = cgrect(x: 0.0, y: viewframe.height - shadowradius, width: viewframe.width, height: shadowradius) case .left: startpoint = cgpoint(x: 0.0, y: 0.5) endpoint = cgpoint(x: 1.0, y: 0.5) self.frame = cgrect(x: 0.0, y: 0.0, width: shadowradius, height: viewframe.height) case .right: startpoint = cgpoint(x: 1.0, y: 0.5) endpoint = cgpoint(x: 0.0, y: 0.5) self.frame = cgrect(x: viewframe.width - shadowradius, y: 0.0, width: shadowradius, height: viewframe.height) } } required public init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
to use it,
let topshadow = edgeshadowlayer(forview: targetview, edge: .top) view.layer.addsublayer(topshadow)
note defaults black-to-white gradient that's 20 pixels deep.
the full code, sample uiviewcontroller
lets toggle shadows on 4 corners of view, can found @ https://github.com/jrtibbetts/tenebrae. i've documented edgeshadowlayer
pretty thoroughly.