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 中运行 Edit > Refactor > Convert to Objective-C ARC... 工具,并确保要使用 ARC 的所有文件都已勾选(包括 FXBlurView.m)。
要使用 FXBlurView,只需将类文件拖入您的项目,并添加 Accelerate 框架。您可以通过编程创建 FXBlurView 实例,或者通过将普通 UIView 拖入您的视图并将其类设置为 FXBlurView 在 Interface Builder 中创建它们。
如果您正在使用 Interface Builder,要设置 FXBlurView 的自定义属性(那些常规 UIView 不支持属性),您可以创建一个 IBOutlet 并在代码中设置属性,或者使用 Interface Builder 中的 User Defined Runtime Attributes 功能(在 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 及以下版本的模糊(与 iOS 7 模糊视图行为保持一致)很有用。默认情况下启用模糊。
+ (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 资源,因此在执行动画之前应始终禁用动态视图以避免卡顿。然而,如果您在屏幕上具有多个 FXBlurView,则使用 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中创建的FXBlurView默认会有蓝色着色。
@property (nonatomic, weak) UIView *underlyingView;
此属性指定FXBlurView将要采样以创建模糊效果的视图。如果设置为nil(默认值),这将是从模糊视图本身的父视图开始的,但如果需要,您可以覆盖此值。
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