
效果
虛化效果主要代碼部分
1、主要是通過動畫控制圖層類 UIViewPropertyAnimator進(jìn)行操作。
2、UIViewPropertyAnimator類具有控制動畫狀態(tài)機控制動畫開始、暫停、停止等操作。
例如:
開始方法:func animator.startAnimation()
暫停方法:func pauseAnimation()
停止方法:func stopAnimation(withoutFinishing: Bool)
完成方法:func finishAnimation(at: UIViewAnimatingPosition)
過程屬性: var fractionComplete:CGFloat // 不需要執(zhí)行開始方法,設(shè)置這個比例值就可以顯示動畫的其中的一個過程
整個動畫控制過程是這樣的:
開始->暫停->停止->完成
3、虛化效果通過UIBlurEffect類完成。就是一個虛化類,沒了
class HeaderView: UICollectionReusableView {
let imageView:UIImageView = {
let iv = UIImageView(image: #imageLiteral(resourceName: "bibi"))
iv.contentMode = .scaleAspectFill
return iv
}()
let cimageView:UIImageView = {
let iv = UIImageView(image: #imageLiteral(resourceName: "ic_wechat_customer"))
iv.contentMode = .scaleAspectFill
return iv
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .blue
translatesAutoresizingMaskIntoConstraints = false
addSubview(imageView)
imageView.fillSuperview()
setupVisualEffectBlur()
addSubview(cimageView)
cimageView.center = center
}
var animator:UIViewPropertyAnimator!
private func setupVisualEffectBlur(){
animator = UIViewPropertyAnimator(duration: 3.0, curve: .easeInOut, animations: {
// 最終動畫的結(jié)果狀態(tài)
let blurEffect = UIBlurEffect(style: .regular) // 虛化效果
let visualEffectView = UIVisualEffectView(effect: blurEffect)
self.addSubview(visualEffectView)
visualEffectView.fillSuperview()
// 縮放
self.cimageView.transform = CGAffineTransform.init(scaleX: 3, y: 3)
})
animator.fractionComplete = 0
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
演示代碼地址:https://github.com/biostome/Stretchy
文獻(xiàn):
快速入門指南:使用 UIViewPropertyAnimator 做動畫
Advanced Animations: UIViewPropertyAnimator Blur Effect (Ep 1)