From 42baf10ef366fb05323788670e8670f525b6ca64 Mon Sep 17 00:00:00 2001 From: seongmin221 Date: Thu, 5 Jan 2023 22:33:50 +0900 Subject: [PATCH] [CHORE] trying out gradient blur (#255) --- .../Global/Extension/UIView+Extension.swift | 44 +++++++++++++++++++ .../Screen/Home/Home/HomeViewController.swift | 1 + 2 files changed, 45 insertions(+) diff --git a/Maddori.Apple/Maddori.Apple/Global/Extension/UIView+Extension.swift b/Maddori.Apple/Maddori.Apple/Global/Extension/UIView+Extension.swift index f1e90b0b..b57bf3ef 100644 --- a/Maddori.Apple/Maddori.Apple/Global/Extension/UIView+Extension.swift +++ b/Maddori.Apple/Maddori.Apple/Global/Extension/UIView+Extension.swift @@ -8,6 +8,14 @@ import UIKit extension UIView { + + enum GradientDirection { + case positiveX + case negativeX + case positiveY + case negativeY + } + func setGradient(colorTop: UIColor, colorBottom: UIColor) { let gradient: CAGradientLayer = CAGradientLayer() gradient.colors = [colorTop.cgColor, colorBottom.cgColor] @@ -18,6 +26,42 @@ extension UIView { layer.addSublayer(gradient) } + func setBlurGradient(in direction: GradientDirection, by portion: CGFloat) { + let gradientLayer = CAGradientLayer() + gradientLayer.colors = [ + UIColor.backgroundWhite.withAlphaComponent(0).cgColor, + UIColor.backgroundWhite.withAlphaComponent(1).cgColor + ] + let viewEffect = UIBlurEffect(style: .light) + let effectView = UIVisualEffectView(effect: viewEffect) + switch direction { + case .positiveX: + effectView.frame = CGRect(x: self.bounds.origin.x, y: self.bounds.origin.y, width: self.bounds.size.width * portion, height: self.bounds.size.height) + effectView.autoresizingMask = [.flexibleWidth] + gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0) + gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0) + case .negativeX: + effectView.frame = CGRect(x: self.bounds.origin.x, y: self.bounds.origin.y, width: self.bounds.size.width * portion, height: self.bounds.size.height) + effectView.autoresizingMask = [.flexibleWidth] + gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.0) + gradientLayer.endPoint = CGPoint(x: 0.0, y: 0.0) + case .positiveY: + effectView.frame = CGRect(x: self.bounds.origin.x, y: self.bounds.origin.y, width: self.bounds.size.width, height: self.bounds.size.height * portion) + effectView.autoresizingMask = [.flexibleHeight] + gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0) + gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0) + case .negativeY: + effectView.frame = CGRect(x: self.bounds.origin.x, y: self.bounds.origin.y, width: self.bounds.size.width, height: self.bounds.size.height * portion) + effectView.autoresizingMask = [.flexibleHeight] + gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0) + gradientLayer.endPoint = CGPoint(x: 0.0, y: 0.0) + } + gradientLayer.frame = effectView.frame + effectView.layer.mask = gradientLayer + effectView.isUserInteractionEnabled = false + addSubview(effectView) + } + @discardableResult func makeShadow(color: UIColor, opacity: Float, diff --git a/Maddori.Apple/Maddori.Apple/Screen/Home/Home/HomeViewController.swift b/Maddori.Apple/Maddori.Apple/Screen/Home/Home/HomeViewController.swift index f1726e39..f38d1003 100644 --- a/Maddori.Apple/Maddori.Apple/Screen/Home/Home/HomeViewController.swift +++ b/Maddori.Apple/Maddori.Apple/Screen/Home/Home/HomeViewController.swift @@ -426,6 +426,7 @@ final class HomeViewController: BaseViewController { } self.flowLayout.count = reflectionKeywordList.count self.keywordCollectionView.reloadData() +// self.keywordCollectionView.setBlurGradient(in: .negativeY, by: 1) } } }