Less Blur with `Visual Effect View with Blur`?

后端 未结 11 1308
死守一世寂寞
死守一世寂寞 2020-12-08 10:11

Title pretty much asks it all...

I\'m playing with the iOS8 Visual Effect View with Blur. It\'s over a UIImageView that shows a user choosa

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 10:46

    It's a pity that Apple did not provide any options for blur effect. But this workaround worked for me - animating the blur effect and pausing it before completion.

    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
            }
        }
    }
    

    and the UIView extensions for pausing (with a delay) and resuming view's animation

    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
        }
    }
    

提交回复
热议问题