FXBlurView 是一个 UIView 子类,它可以复制 iOS 7 实时背景模糊效果,但可以在 iOS 5 及以上版本上运行。它被设计成尽可能快速和简单易用。FXBlurView 提供两种操作模式:静态模式,在将视图添加到父视图时只渲染一次(虽然可以通过调用 setNeedsDisplay 更新视图);动态模式,在后台线程上尽可能频繁地自动重新绘制自己。
注意:'支持' 的意思是这个库已经与这个版本进行了测试。'兼容' 的意思是这个库应该可以在这个 iOS 版本上运行(即它不依赖任何不可用的 SDK 功能),但不再进行兼容性测试,可能需要调整或错误修复才能正确运行。
从版本 1.3 开始,FXBlurView 需要 ARC。如果您希望在非 ARC 项目中使用 FXBlurView,只需将 -fobjc-arc 编译器标志添加到 FXBlurView.m 类中。为此,转到目标设置的构建阶段选项卡,打开编译源组,在列表中双击 FXBlurView.m,并在弹出菜单中输入 -fobjc-arc。
如果您希望将整个项目转换为 ARC,请注释掉 FXBlurView.m 中的 #error 行,然后运行 Xcode 中的 Edit > Refactor > Convert to Objective-C ARC... 工具,并确保所有要使用 ARC 的文件(包括 FXBlurView.m)都已被选中。
要使用 FXBlurView,只需将类文件拖放到项目中并添加 Accelerate 框架。您可以通过编程创建 FXBlurView 实例,或者通过在 Interface Builder 中将普通的 UIView 拖放到您的视图中并将其类设置为 FXBlurView 创建它们。
如果您使用 Interface Builder,要设置 FXBlurView 的自定义属性(不支持普通 UIView 的那些属性),可以通过为您的视图创建 IBOutlet 并在代码中设置属性,或者使用 Interface Builder 中的用户定义运行时属性功能(从 Xcode 4.2 和 iOS 5 开始引入)。
FXBlurView扩展了UIImage,并具有以下方法:
- (UIImage *)blurredImageWithRadius:(CGFloat)radius
iterations:(NSUInteger)iterations
tintColor:(UIColor *)tintColor;
此方法应用模糊效果并返回结果图像,而不修改原始图像。radius属性控制模糊效果的强度。iterations属性控制迭代次数。迭代次数越多,质量越高。tintColor是一个可选颜色,将与其结果图像混合。请注意,tintColor的alpha组件将忽略。
+ (void)setBlurEnabled:(BOOL)blurEnabled;
此方法可以用于全局启用/禁用所有FXBlurView实例的模糊效果。这在测试或您希望禁用iPhone 4及以下设备的模糊效果时非常有用(与iOS7模糊视图行为保持一致)。默认情况下,模糊效果已启用。
+ (void)setUpdatesEnabled;
+ (void)setUpdatesDisabled;
这些方法可以用于使用单个命令启用和禁用所有动态FXBlurView实例的更新。在执行动画之前立即禁用更新,这样FXBlurView的更新不会导致动画卡顿。调用可以嵌套,但请确保启用/禁用调用平衡,否则更新将永久启用或禁用。
- (void)setNeedsDisplay;
从UIView继承,此方法可以用于触发视图的(同步)更新。当dynamic = NO
时非常有用。
@property (nonatomic, getter = isBlurEnabled) BOOL blurEnabled;
此属性切换单个FXBlurView实例的模糊开启和关闭。默认情况下启用模糊。请注意,如果您使用+setBlurEnabled
方法禁用模糊,则将覆盖此设置。
@property (nonatomic, getter = isDynamic) BOOL dynamic;
此属性控制FXBlurView是否动态更新,或者仅在将其视图添加到父视图时更新一次。默认为YES。请注意,如果将dynamic设置为NO,您仍然可以通过调用setNeedsDisplay强制视图更新。动态模糊非常消耗CPU资源,因此您应该始终在执行动画之前立即禁用动态视图以避免卡顿。然而,如果您在屏幕上有多个FXBlurView,则使用setUpdatesDisabled
方法禁用更新比将dynamic
属性设置为NO更简单。
@property (nonatomic, assign) NSUInteger iterations;
模糊迭代次数。迭代次数越多,质量越好,但性能降低。默认为2次迭代。
@property (nonatomic, assign) NSTimeInterval updateInterval;
此属性控制动态模式下FXBlurView连续更新之间的间隔(以秒为单位)。默认为零,这意味着FXBlurView将尽可能地快速更新。这会带来最佳的帧率,但也是极高性能消耗的,可能会造成您的应用整体性能下降,尤其是在旧设备上。为了减轻这一点,请尝试增加updateInterval
值。
@property (nonatomic, assign) CGFloat blurRadius;
此属性控制模糊效果的半径(以点为单位)。默认为40点半径,类似于iOS 7模糊效果。
@property (nonatomic, strong) UIColor *tintColor;
这是一个可选的着色颜色,可应用于FXBlurView。颜色的RGB成分将与模糊图像混合,形成微妙的着色。要改变着色效果的强度,请使用更亮或更暗的颜色。tintColor的alpha组件将被忽略。如果您不想应用着色,则将此值设置为nil或[UIColor clearColor]。请注意,如果您正在使用Xcode 5或更高版本,则在Interface Builder中创建的FXBlurView默认会有蓝色着色。
Q. Why are my views all blue-tinted on iOS 7?
A. FXBlurView uses the `UIView` `tintColor` property, which does not exist on iOS 6 and below, but defaults to blue on iOS 7. Just set this property to `[UIColor clearColor]` to disable the tint. To retain iOS 6 compatibility, you can either set this using code, or by using the User Defined Runtime Attributes feature of Interface Builder, which will override the standard `tintColor` value (see the example project nibs for how to do this).
Q. FXBlurView makes my whole app run slowly on [old device], what can I do
A. To improve performance, try increasing the `updatePeriod` property, reducing the `iterations` property or disabling `dynamic` unless you really need it. If all else fails, set `blurEnabled` to NO on older devices.
Q. My SpriteKit/OpenGL/Video/3D transformed content isn't showing up properly when placed underneath an FXBlurView, why not?
A. This is a limitation of a the `CALayer` `renderInContext:` method used to capture the superview contents. There is no workaround for this on iOS 6 and earlier. On iOS 7 you can make use of the `UIView` `drawViewHierarchyInRect:afterScreenUpdates:` method to capture an view and apply the blur effect yourself, but this it too slow for realtime use, so FXBlurView does not use this method by default.
Q. FXBlurView is not capturing some ordinary view content that is behind it, why not?
A. FXBlurView only captures the contents of its immediate superview. If the superview is transparent or partially transparent, content shown behind it will not be captured.