CALayer擁有mask屬性,Apple的官方解釋如下:
An optional layer whose alpha channel is used to mask the layer’s content. The layer’s alpha channel determines how much of the layer’s content and background shows through. Fully or partially opaque pixels allow the underlying content to show through but fully transparent pixels block that content.
mask同樣也是一個CALayer。假設(shè)將CALayer本身稱為ContentLayer离陶,將mask稱為MaskLayer于游,蒙版(Masking)的工作原理是通過MaskLayer的alpha值定義ContentLayer的顯示區(qū)域:對于ContentLayer上每一個Point鉴象,計(jì)算公式為ResultLayer = ContentLayer * MaskLayer_Alpha惋增。所以當(dāng)alpha為1時Content顯示,alpha為0時Content不顯示甩骏,其他處于0與1之間的值導(dǎo)致Content半透明。
需要注意的是:
MaskLayer的color不重要先慷,主要使用opacity(CALayer中的alpha)饮笛,但是注意[UIColor clearColor]其實(shí)就是alpha為0的color。
ContentLayer超出MaskLayer以外的部分不會被顯示出來论熙。
MaskLayer不能有superLayer福青,否則蒙版(Masking)的結(jié)果就是未知(Undefined)。
由于mask是一個CALayer脓诡,可以通過組合產(chǎn)生很多非常棒的效果素跺。例如可以將MaskLayer指定為CAGradientLayer類型實(shí)現(xiàn)Gradient效果,可以給MaskLayer添加動畫誉券。
下面是自己的用Mask做的Splash Demo:Demo 里面 15-AnimatedSplash項(xiàng)目指厌。