Skip to content

UIVisualEffect ​

UIVisualEffect 表示一种视觉效果,是四种效果的共同父类:UIBlurEffect(模糊)、UIVibrancyEffect(鲜明)、UIGlassEffect(玻璃)、UIGlassContainerEffect(玻璃容器)。

效果对象自己不显示任何东西,交给 UIVisualEffectView 才看得见。UIVisualEffect 没有成员,它的用处是做类型:效果视图的 effect 属性接受任何一种效果。

继承关系:EventTarget → NSObject → UIVisualEffect

构造函数 ​

不能用 new 创建。用四个子类的工厂方法或构造函数创建效果:

类怎么得到
UIBlurEffectUIBlurEffect.effectWithStyle()
UIVibrancyEffectUIVibrancyEffect.effectForBlurEffect()
UIGlassEffectUIGlassEffect.effectWithStyle()
UIGlassContainerEffectnew UIGlassContainerEffect()

示例 ​

js
/** @param {UIVisualEffectView} view */
function describe(view) {
  const effect = view.effect;
  if (effect === null) return '没有效果';
  if (effect instanceof UIBlurEffect) return '模糊';
  if (effect instanceof UIGlassEffect) return '玻璃';
  return '别的效果';
}
console.log(describe(new UIVisualEffectView(UIBlurEffect.effectWithStyle('dark'))));

结果:输出 模糊。用 instanceof 判断效果的种类未在模拟器验证。

对应的 iOS API ​

UIVisualEffect。

参见 ​