FXBlurView 是一个 UIView 子类,它复制了 iOS 7 实时背景模糊效果,但可以在 iOS 5 及以上版本的操作系统上运行。它被设计成尽可能快和简单易用。FXBlurView 提供两种操作模式:静态模式,视图在被添加到父视图时只渲染一次(尽管可以通过调用 setNeedsDisplay
或 updateAsynchronously:completion:
更新)或动态模式,视图将在后台线程尽可能多地自动重新绘制自己。
注:'支持的'意味着库已测试与该版本兼容。'兼容'意味着库应该在该 iOS 版本上运行(即它不依赖任何不可用的 SDK 功能),但不能保证兼容性,可能需要调整或修复错误以正确运行。
从版本 1.3 开始,FXBlurView 需要 ARC。如果您想在非 ARC 项目中使用 FXBlurView,只需将 -fobjc-arc 编译器标志添加到 FXBlurView.m 类中。为此,请进入目标设置的“构建阶段”选项卡,打开“编译源”组,在列表中对 FXBlurView.m 进行双击,并在弹出窗口中输入 -fobjc-arc。
如果您要将整个项目转换为 ARC,请将 FXBlurView.m 中的 #error 行注释掉,然后在 Xcode 中运行“编辑”>“重构”>“转换为 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 *)blurredImageWithRadius:(CGFloat)radius
iterations:(NSUInteger)iterations
tintColor:(UIColor *)tintColor;
此方法应用模糊效果并返回结果模糊图像,而不修改原始图像。半径属性控制模糊效果的强度。迭代属性控制迭代次数。迭代次数越多,质量越高。tintColor是一个可选的颜色,将与其结果图像混合。注意,tintColor的alpha分量将被忽略。
+ (void)setBlurEnabled:(BOOL)blurEnabled;
此方法可用于全局启用/禁用所有 FXBlurView 实例的模糊效果。这在进行测试或希望禁用 iPhone 4 及以下版本的模糊效果时很有用(以与 iOS7 模糊视图的行为保持一致)。默认情况下模糊效果是启用的。
+ (void)setUpdatesEnabled;
+ (void)setUpdatesDisabled;
这些方法可以通过单个命令启用和禁用所有动态 FXBlurView 实例的更新。在执行动画之前立即禁用更新可以防止 FXBlurView 的更新导致动画停滞。调用可以嵌套,但请确保启用/禁用调用是平衡的,否则更新将永久启用或禁用。
- (void)updateAsynchronously:(BOOL)async completion:(void (^)())completion;
此方法可用于触发模糊效果的更新(当 dynamic = NO
时很有用)。async 参数控制模糊是否在主线程或者后台线程重绘。completion 参数是一个可选的回调块,当模糊完成后将被调用。
- (void)setNeedsDisplay;
此方法继承自 UIView,可用于触发视图的(同步)更新。调用此方法在某种程度上等同于调用 [view updateAsynchronously:NO completion:NULL]
。
@property (nonatomic, getter = isBlurEnabled) BOOL blurEnabled;
此属性切换单个 FXBlurView 实例的模糊效果的开/关。默认情况下模糊效果是启用的。注意,如果你使用 +setBlurEnabled
方法禁用模糊,则将覆盖此设置。
@property (nonatomic, getter = isDynamic) BOOL dynamic;
此属性控制 FXBlurView 是否动态更新,或者仅在视图添加到父视图时一次性更新。默认值为 YES。注意,如果将 dynamic 设置为 NO,你仍然可以通过调用 setNeedsDisplay
或 updateAsynchronously:completion:
来强制视图更新。动态模糊非常占用 CPU 资源,因此你应该在执行动画前立即禁用动态视图以避免卡顿。然而,如果你屏幕上有多个 FXBlurViews,使用 setUpdatesDisabled
方法禁用更新而不是将 dynamic
属性设置为 NO 会更加简单。
@property (nonatomic, assign) NSUInteger iterations;
模糊迭代的次数。迭代次数越多,质量越好,但性能降低。默认值为 2 次迭代。
@property (nonatomic, assign) NSTimeInterval updateInterval;
此属性控制 FXBlurView 在动态模式下的连续更新之间的间隔(以秒为单位)。默认值为零,意味着 FXBlurView 将尽可能快地更新。这产生最佳帧率,但也是极其占用 CPU 的,可能会导致你的应用程序性能下降,尤其是在旧设备上。为了减轻这一点,尝试增加 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 中创建的 FXBlurViews 将默认具有蓝色着色。
@property (nonatomic, weak) UIView *underlyingView;
此属性指定 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 view 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 captures the contents of its immediate superview by default. If the superview is transparent or partially transparent, content shown behind it will not be captured. You can override the `underlyingView` property to capture the contents of a different view if you need to.
版本 1.6.3
版本 1.6.2
版本 1.6.1
版本 1.6
版本 1.5.6
版本 1.5.5
版本 1.5.4
版本 1.5.3
版本 1.5.2
版本 1.5.1
版本 1.5
版本 1.4.4
版本 1.4.3
版本 1.4.2
版本 1.4.1
版本 1.4
版本 1.3.3
版本 1.3.2
版本 1.3.1
版本 1.3
版本 1.2
版本 1.1
版本 1.0