ios - CALayer with transparent hole in it -
i have simple view (left side of picture) , need create kind of overlay (right side of picture) view. overlay should have opacity, view bellow still partly visible. importantly overlay should have circular hole in middle of doesn't overlay center of view (see picture bellow).
i can create circle :
int radius = 20; //whatever cashapelayer *circle = [cashapelayer layer]; circle.path = [uibezierpath bezierpathwithroundedrect:cgrectmake(0, 0,radius,radius) cornerradius:radius].cgpath; circle.position = cgpointmake(cgrectgetmidx(view.frame)-radius, cgrectgetmidy(view.frame)-radius); circle.fillcolor = [uicolor clearcolor].cgcolor;
and "full" rectangular overlay :
cashapelayer *shadow = [cashapelayer layer]; shadow.path = [uibezierpath bezierpathwithroundedrect:cgrectmake(0, 0, view.bounds.size.width, view.bounds.size.height) cornerradius:0].cgpath; shadow.position = cgpointmake(0, 0); shadow.fillcolor = [uicolor graycolor].cgcolor; shadow.linewidth = 0; shadow.opacity = 0.5; [view.layer addsublayer:shadow];
but have no idea how can combine these 2 layers create effect want. anyone? i've tried everything... lot help!
i able solve jon steinmetz suggestion. if 1 cares, here's final solution :
int radius = myrect.size.width; uibezierpath *path = [uibezierpath bezierpathwithroundedrect:cgrectmake(0, 0, self.mapview.bounds.size.width, self.mapview.bounds.size.height) cornerradius:0]; uibezierpath *circlepath = [uibezierpath bezierpathwithroundedrect:cgrectmake(0, 0, 2.0*radius, 2.0*radius) cornerradius:radius]; [path appendpath:circlepath]; [path setusesevenoddfillrule:yes]; cashapelayer *filllayer = [cashapelayer layer]; filllayer.path = path.cgpath; filllayer.fillrule = kcafillruleevenodd; filllayer.fillcolor = [uicolor graycolor].cgcolor; filllayer.opacity = 0.5; [view.layer addsublayer:filllayer];
for swift 3.x:
let radius = myrect.size.width let path = uibezierpath(roundedrect: cgrect(x: 0, y: 0, width: self.mapview.bounds.size.width, height: self.mapview.bounds.size.height), cornerradius: 0) let circlepath = uibezierpath(roundedrect: cgrect(x: 0, y: 0, width: 2 * radius, height: 2 * radius), cornerradius: radius) path.append(circlepath) path.usesevenoddfillrule = true let filllayer = cashapelayer() filllayer.path = path.cgpath filllayer.fillrule = kcafillruleevenodd filllayer.fillcolor = color.background.cgcolor filllayer.opacity = 0.5 view.layer.addsublayer(filllayer)
Comments
Post a Comment