ios - Substract CALayer from another, blend? -
on next image, bluish circle debug purpose. goal every layer behind bluish circle should transparent.i want maintain visible what's outside bluish circle.
here code written in swift :
allow croissantview = uiview(frame: cgrectmake(100, 100, 100, 100)) croissantview.backgroundcolor = uicolor.clearcolor() self.view.addsubview(croissantview) allow radius = 50 cgfloat allow width = 50 cgfloat allow origin = cgpointmake(0, 100) allow end = cgpointmake(100,100) allow highamplitude = cgpointmake(50,180) allow lowamplitude = cgpointmake(50,150) allow quad = uibezierpath() quad.movetopoint(origin) quad.addquadcurvetopoint(end, controlpoint: highamplitude) quad.closepath() allow quad2 = uibezierpath() quad2.movetopoint(origin) quad2.addquadcurvetopoint(end, controlpoint: lowamplitude) quad2.closepath() allow layer = cashapelayer() layer.path = quad.cgpath layer.strokecolor = uicolor.greencolor().cgcolor layer.fillcolor = uicolor.blackcolor().cgcolor croissantview.layer.addsublayer(layer) allow anim = cabasicanimation(keypath: "path") anim.duration = 3 anim.repeatcount = 20 anim.fromvalue = quad.cgpath anim.tovalue = quad2.cgpath anim.autoreverses = true layer.addanimation(anim, forkey: "animquad") allow animrotate = cabasicanimation(keypath: "transform.rotation") animrotate.duration = 5 animrotate.repeatcount = 20 animrotate.fromvalue = 0 animrotate.tovalue = 360 * cgfloat(m_pi) / 180 croissantview.layer.addanimation(animrotate, forkey: "animrotate") allow circle = uibezierpath(arccenter: croissantview.center, radius: 75, startangle: 0, endangle: 360 * cgfloat(m_pi) / 180, clockwise: true); allow circlelayer = cashapelayer() circlelayer.path = circle.cgpath circlelayer.fillcolor = uicolor.bluecolor().cgcolor circlelayer.opacity = 0.6 self.view.layer.addsublayer(circlelayer)
thoughts substract layer, don't know if feasible blend, don't know how calayer thanks !! :)
there 3 things suggest.
change croissant view layer (not sure necessary, did)
create cgpath circle shape can have mask inverted. wrapping outer rect around circle path looks this:
set fill rule on circle shape layer even-odd:
circlelayer.fillrule = kcafillruleevenodd
this looks like:
i adapted code effort , came with. mileage may vary:
func applymask() { allow mainlayer = calayer() mainlayer.bounds = cgrect(x: 0.0, y: 0.0, width: 100.0, height: 100.0) mainlayer.position = cgpoint(x: 200.0, y: 200.0) // set color debugging mainlayer.backgroundcolor = uicolor.orangecolor().cgcolor self.view.layer.addsublayer(mainlayer) allow radius = 50 cgfloat allow width = 50 cgfloat allow origin = cgpointmake(0, 100) allow end = cgpointmake(100,100) allow highamplitude = cgpointmake(50,180) allow lowamplitude = cgpointmake(50,150) allow quad = uibezierpath() quad.movetopoint(origin) quad.addquadcurvetopoint(end, controlpoint: highamplitude) quad.closepath() allow quad2 = uibezierpath() quad2.movetopoint(origin) quad2.addquadcurvetopoint(end, controlpoint: lowamplitude) quad2.closepath() allow layer = cashapelayer() layer.path = quad.cgpath layer.strokecolor = uicolor.greencolor().cgcolor layer.fillcolor = uicolor.blackcolor().cgcolor mainlayer.addsublayer(layer) allow anim = cabasicanimation(keypath: "path") anim.duration = 3 anim.repeatcount = 20 anim.fromvalue = quad.cgpath anim.tovalue = quad2.cgpath anim.autoreverses = true layer.addanimation(anim, forkey: "animquad") allow animrotate = cabasicanimation(keypath: "transform.rotation") animrotate.duration = 5 animrotate.repeatcount = 20 animrotate.fromvalue = 0 animrotate.tovalue = 360 * cgfloat(m_pi) / 180 mainlayer.addanimation(animrotate, forkey: "animrotate") // build circle cgpath var circlepath = cgpathcreatemutable() cgpathaddellipseinrect(circlepath, nil, cgrectinset(mainlayer.bounds, -20.0, -20.0)) // invert mask adding bounding rectangle cgpathaddrect(circlepath, nil, cgrectinset(mainlayer.bounds, -100.0, -100.0)) cgpathclosesubpath(circlepath) allow circlelayer = cashapelayer() circlelayer.fillcolor = uicolor.bluecolor().cgcolor circlelayer.opacity = 0.6 // utilize odd fill rule our path gets inverted circlelayer.fillrule = kcafillruleevenodd circlelayer.path = circlepath mainlayer.mask = circlelayer mainlayer.addsublayer(layer) }
here's project tried out: https://github.com/perlmunger/swiftvertedmask
hope helps.
ios swift core-graphics core-animation calayer
No comments:
Post a Comment