ios - CALayer does not respect zero shadowRadius while drawing? -
it seems calayer's property shadowradius non-zero while drawing if explicitelly set 0 when following conditions met:
shadowpathproperty set on samecalayer, andshadowpathset else plain rectangular path
to reproduce issue created 2 rounded calayers shadow identical in shadow path - first 1 has shadowpath set nil, second 1 has shadowpath set own shape. expect 2 render same picture not. here result (magnified):

as can see second rectangle has shadow radius higher 0 though set 0. here code used produce picture above:
override func viewdidload() { super.viewdidload() let layerwithoutshadowpath = calayer() //shadow of rectangle drawn correctly let layerwithshadowpath = calayer() //shadow of 1 drawn radius higher 0 layerwithoutshadowpath.frame = cgrect(x: 30, y: 30, width: 30, height: 30) layerwithshadowpath.frame = cgrect(x: 70, y: 30, width: 30, height: 30) layerwithshadowpath.shadowpath = uibezierpath(roundedrect: layerwithshadowpath.bounds, cornerradius: 4.0).cgpath setuplayer(layerwithoutshadowpath) setuplayer(layerwithshadowpath) } private func setuplayer(layer: calayer) { layer.backgroundcolor = uicolor.yellowcolor().colorwithalphacomponent(1).cgcolor layer.cornerradius = 4.0 layer.shadowcolor = uicolor.blackcolor().cgcolor layer.shadowoffset = cgsize(width: 0, height: 3) layer.shadowradius = 0 //shadow radius set 0 both rectangles layer.shadowopacity = 1 view.layer.addsublayer(layer) } this might cocoa bug or missing something... anyway, question is: how can add rounded rectangle shadow 0 radius calayer while shadowpath set? (if wondering why must set shadowpath the reason performance)
well, might able fix setting layer's edgeantialiasingmask. however, seems me needlessly trapped in world of layer inefficiency - if thing knew how make layer draw rounded corners , shadow. i draw yellow rounded square and shadow actual drawing, , make drawing content of layer:
let layer = calayer() layer.contentsscale = uiscreen.mainscreen().scale layer.frame = cgrect(x: 30, y: 30, width: 30, height: 35) uigraphicsbeginimagecontextwithoptions(layer.bounds.size, false, 0) let con = uigraphicsgetcurrentcontext() cgcontextsetshadowwithcolor(con, cgsizemake(0,3), 1, uicolor.blackcolor().cgcolor) let path = uibezierpath(roundedrect: cgrectmake(0,0,30,30), cornerradius: 4) uicolor.yellowcolor().setfill() path.fill() let im = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() layer.contents = im.cgimage view.layer.addsublayer(layer) this has advantage shadow part of drawing, , there no inefficiency - inefficiency trying avoid - there if give layer shadow , compelled render tree work. indeed, same thing true rounded rectangle: more efficient draw rounded rectangle, doing here, force render tree round layer's corners you, doing.
Comments
Post a Comment