Tra*_*ggs 26 objective-c ios ios8 uiblureffect uivisualeffectview
标题几乎要求一切......
我正在玩iOS8 Visual Effect View with Blur.它UIImageView显示了用户可选择的背景照片.放置在contentView本身的视图是我自己的自定义视图,显示了一种图形/日历.大多数所述日历图是透明的.适用于背后照片的模糊非常沉重.我想让更多细节泄漏.但苹果似乎只给出了三个罐头价值:
typedef enum {
UIBlurEffectStyleExtraLight,
UIBlurEffectStyleLight,
UIBlurEffectStyleDark
} UIBlurEffectStyle;
Run Code Online (Sandbox Code Playgroud)
我已经用不同的alphas和背景颜色逛了一圈UIVisualEffectView(尽管文档警告说),但这并没有做任何事情,只会让事情变得更糟.
mit*_*a13 46
遗憾的是Apple没有为模糊效果提供任何选项.但这个解决方法对我有用 - 动画模糊效果并在完成之前暂停它.
func blurEffectView(enable enable: Bool) {
let enabled = self.blurView.effect != nil
guard enable != enabled else { return }
switch enable {
case true:
let blurEffect = UIBlurEffect(style: .ExtraLight)
UIView.animateWithDuration(1.5) {
self.blurView.effect = blurEffect
}
self.blurView.pauseAnimation(delay: 0.3)
case false:
self.blurView.resumeAnimation()
UIView.animateWithDuration(0.1) {
self.blurView.effect = nil
}
}
}
Run Code Online (Sandbox Code Playgroud)
以及用于暂停(延迟)和恢复视图动画的UIView扩展
extension UIView {
public func pauseAnimation(delay delay: Double) {
let time = delay + CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, time, 0, 0, 0, { timer in
let layer = self.layer
let pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
})
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
}
public func resumeAnimation() {
let pausedTime = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
}
}
Run Code Online (Sandbox Code Playgroud)
Sti*_*sis 35
您变得模糊的原因是模糊效果样式会影响图像的亮度级别,而不会影响应用的模糊量.

不幸的是,虽然Apple显然能够以编程方式控制应用的模糊量 - 尝试在启动板上慢慢拖动以观察Spotlight模糊转换 - 我没有看到任何公共API来传递模糊量UIBlurEffect.
这篇文章声称调整背景颜色alpha将驱动模糊量.值得一试,但我没有看到记录的位置:如何淡入和淡出UIVisualEffectView和/或UIBlurEffect?
小智 8
这适合我.
我把UIVisualEffectView在UIView之前添加到我的观点.
我使这个功能更容易使用.您可以使用此功能模糊视图中的任何区域.
func addBlurArea(area: CGRect, style: UIBlurEffectStyle) {
let effect = UIBlurEffect(style: style)
let blurView = UIVisualEffectView(effect: effect)
let container = UIView(frame: area)
blurView.frame = CGRect(x: 0, y: 0, width: area.width, height: area.height)
container.addSubview(blurView)
container.alpha = 0.8
self.view.insertSubview(container, atIndex: 1)
}
Run Code Online (Sandbox Code Playgroud)
例如,您可以通过调用以下方式使所有视图模糊:
addBlurArea(self.view.frame, style: UIBlurEffectStyle.Dark)
Run Code Online (Sandbox Code Playgroud)
您可以更改Dark为所需的模糊样式和0.8所需的Alpha值
类似于此处的一些但更简单的解决方案是使用 UIViewPropertyAnimator (iOS 10+) 并将其fractionComplete属性设置为 0 到 1 之间的某个值。
// add blur view to image view
let imgBlur = UIVisualEffectView()
imgView.addSubview(imgBlur)
imgBlur.frame = imgView.bounds
// create animator to control blur strength
let imgBlurAnimator = UIViewPropertyAnimator()
imgBlurAnimator.addAnimations {
imgBlur.effect = UIBlurEffect(style: .dark)
}
// 50% blur
imgBlurAnimator.fractionComplete = 0.5
Run Code Online (Sandbox Code Playgroud)
请注意,如果您打算fractionComplete根据平移手势、滚动视图、滑块等进行更改,则需要设置pausesOnCompletion = true(iOS 11+)。
| 归档时间: |
|
| 查看次数: |
29847 次 |
| 最近记录: |