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 中的用户定义运行时属性功能(在 Xcode 4.2 中为 iOS 5 引入)。
FXBlurView 通过以下方法扩展了 UIImage:
- (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。请注意,如果动态设置为 NO,您仍然可以通过调用 setNeedsDisplay
或 updateAsynchronously:completion:
来强制视图更新。动态模糊非常消耗CPU资源,因此在执行动画之前始终应立即禁用动态视图,以避免卡顿。然而,如果有多个 FXBlurViews 在屏幕上,则使用 setUpdatesDisabled
方法禁用更新比将 dynamic
属性设置为 NO 更简单。
@property (nonatomic, assign) NSUInteger iterations;
模糊迭代的次数。迭代次数越多,质量越好,但性能也会降低。默认为 2 次迭代。
@property (nonatomic, assign) NSTimeInterval updateInterval;
此属性控制动态模式中连续更新之间的时间间隔(以秒为单位)。默认为 0,这意味着 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