Skip to content

Commit

Permalink
💫 Update: Exp - AdaptiveModal
Browse files Browse the repository at this point in the history
Summary: Update experiment/test - `swift-programmatic-modal/AdaptiveModal`.
  • Loading branch information
dominicstop committed Jun 16, 2023
1 parent 6a07637 commit fe35017
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,39 @@ import UIKit


struct AdaptiveModalAnimationConfig {
let modalRotation: CGFloat?;
var modalRotation: CGFloat?;

let modalScaleX: CGFloat?;
let modalScaleY: CGFloat?;
var modalScaleX: CGFloat?;
var modalScaleY: CGFloat?;

let modalTranslateX: CGFloat?;
let modalTranslateY: CGFloat?;
var modalTranslateX: CGFloat?;
var modalTranslateY: CGFloat?;

let modalOpacity: CGFloat?;
let modalBackgroundColor: UIColor?;
let modalBackgroundOpacity: CGFloat?;
var modalOpacity: CGFloat?;
var modalBackgroundColor: UIColor?;
var modalBackgroundOpacity: CGFloat?;

let modalCornerRadius: CGFloat?;
let modalMaskedCorners: CACornerMask?;
var modalBorderWidth: CGFloat?;
var modalBorderColor: UIColor?;

let modalBackgroundVisualEffect: UIVisualEffect?;
let modalBackgroundVisualEffectOpacity: CGFloat?;
let modalBackgroundVisualEffectIntensity: CGFloat?;
var modalShadowColor: UIColor?;
var modalShadowOffset: CGSize?;
var modalShadowOpacity: CGFloat?;
var modalShadowRadius: CGFloat?;

let backgroundColor: UIColor?;
let backgroundOpacity: CGFloat?;
var modalCornerRadius: CGFloat?;
var modalMaskedCorners: CACornerMask?;

let backgroundVisualEffect: UIVisualEffect?;
let backgroundVisualEffectOpacity: CGFloat?;
let backgroundVisualEffectIntensity: CGFloat?;
var modalBackgroundVisualEffect: UIVisualEffect?;
var modalBackgroundVisualEffectOpacity: CGFloat?;
var modalBackgroundVisualEffectIntensity: CGFloat?;

var backgroundColor: UIColor?;
var backgroundOpacity: CGFloat?;

var backgroundVisualEffect: UIVisualEffect?;
var backgroundVisualEffectOpacity: CGFloat?;
var backgroundVisualEffectIntensity: CGFloat?;

init(
modalRotation: CGFloat? = nil,
Expand All @@ -44,6 +52,12 @@ struct AdaptiveModalAnimationConfig {
modalOpacity: CGFloat? = nil,
modalBackgroundColor: UIColor? = nil,
modalBackgroundOpacity: CGFloat? = nil,
modalBorderWidth: CGFloat? = nil,
modalBorderColor: UIColor? = nil,
modalShadowColor: UIColor? = nil,
modalShadowOffset: CGSize? = nil,
modalShadowOpacity: CGFloat? = nil,
modalShadowRadius: CGFloat? = nil,
modalCornerRadius: CGFloat? = nil,
modalMaskedCorners: CACornerMask? = nil,
modalBackgroundVisualEffect: UIVisualEffect? = nil,
Expand All @@ -67,6 +81,14 @@ struct AdaptiveModalAnimationConfig {
self.modalBackgroundColor = modalBackgroundColor;
self.modalBackgroundOpacity = modalBackgroundOpacity;

self.modalBorderWidth = modalBorderWidth;
self.modalBorderColor = modalBorderColor;

self.modalShadowColor = modalShadowColor;
self.modalShadowOffset = modalShadowOffset;
self.modalShadowOpacity = modalShadowOpacity;
self.modalShadowRadius = modalShadowRadius;

self.modalCornerRadius = modalCornerRadius;
self.modalMaskedCorners = modalMaskedCorners;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import UIKit

struct AdaptiveModalInterpolationPoint: Equatable {

private static let DefaultMaskedCorners: CACornerMask = [
static let DefaultMaskedCorners: CACornerMask = [
.layerMaxXMinYCorner,
.layerMinXMinYCorner,
.layerMaxXMaxYCorner,
.layerMinXMaxYCorner,
];

// MARK: - Properties
// ------------------

Expand All @@ -40,6 +40,14 @@ struct AdaptiveModalInterpolationPoint: Equatable {
var modalBackgroundColor: UIColor;
var modalBackgroundOpacity: CGFloat;

var modalBorderWidth: CGFloat;
var modalBorderColor: UIColor;

var modalShadowColor: UIColor;
var modalShadowOffset: CGSize;
var modalShadowOpacity: CGFloat;
var modalShadowRadius: CGFloat;

var modalCornerRadius: CGFloat;
var modalMaskedCorners: CACornerMask;

Expand Down Expand Up @@ -81,8 +89,105 @@ struct AdaptiveModalInterpolationPoint: Equatable {
};
};

// MARK: - Init
// ------------
// MARK: - Functions
// -----------------

func getModalTransform(
shouldApplyRotation: Bool = true,
shouldApplyScale: Bool = true,
shouldApplyTranslate: Bool = true
) -> CGAffineTransform {

var transforms: [CGAffineTransform] = [];

if shouldApplyRotation,
self.modalRotation != 0 {

transforms.append(
.init(rotationAngle: self.modalRotation)
);
};

if shouldApplyScale,
self.modalScaleX != 1 && self.modalScaleY != 1 {

transforms.append(
.init(scaleX: self.modalScaleX, y: self.modalScaleY)
);
};

if shouldApplyTranslate,
self.modalTranslateX != 0 && self.modalTranslateY != 0 {

transforms.append(
.init(translationX: self.modalTranslateX, y: self.modalTranslateY)
);
};

if transforms.isEmpty {
return .identity;
};

return transforms.reduce(.identity){
$0.concatenating($1);
};
};

func apply(toModalView modalView: UIView){
modalView.alpha = self.modalOpacity;

modalView.layer.cornerRadius = self.modalCornerRadius;
modalView.layer.maskedCorners = self.modalMaskedCorners;
};

func apply(toModalWrapperView modalWrapperView: UIView){
modalWrapperView.frame = self.computedRect;
};

func apply(toModalWrapperTransformView view: UIView?){
view?.transform = self.modalTransform;
};

func apply(toModalWrapperShadowView view: UIView?){
guard let view = view else { return };

// border
view.layer.borderWidth = self.modalBorderWidth;
view.layer.borderColor = self.modalBorderColor.cgColor;

// shadow
view.layer.shadowColor = self.modalShadowColor.cgColor;
view.layer.shadowOffset = self.modalShadowOffset;
view.layer.shadowOpacity = Float(self.modalShadowOpacity);
view.layer.shadowRadius = self.modalShadowRadius;
};

func apply(toDummyModalView dummyModalView: UIView){
dummyModalView.frame = self.computedRect;
};

func apply(toModalBackgroundView modalBgView: UIView?){
modalBgView?.alpha = self.modalBackgroundOpacity;
modalBgView?.backgroundColor = self.modalBackgroundColor;
};

func apply(toModalBackgroundEffectView effectView: UIVisualEffectView?){
effectView?.alpha = self.modalBackgroundVisualEffectOpacity;
};

func apply(toBackgroundView bgView: UIView?){
bgView?.alpha = self.backgroundOpacity;
};

func apply(toBackgroundVisualEffectView effectView: UIView?){
effectView?.alpha = self.backgroundVisualEffectOpacity;
};
};

// MARK: - Init
// ------------

extension AdaptiveModalInterpolationPoint {

init(
usingModalConfig modalConfig: AdaptiveModalConfig,
Expand Down Expand Up @@ -157,6 +262,30 @@ struct AdaptiveModalInterpolationPoint: Equatable {
self.modalBackgroundOpacity = keyframeCurrent?.modalBackgroundOpacity
?? keyframePrev?.modalBackgroundOpacity
?? 1;

self.modalBorderWidth = keyframeCurrent?.modalBorderWidth
?? keyframePrev?.modalBorderWidth
?? 0;

self.modalBorderColor = keyframeCurrent?.modalBorderColor
?? keyframePrev?.modalBorderColor
?? .black;

self.modalShadowColor = keyframeCurrent?.modalShadowColor
?? keyframePrev?.modalShadowColor
?? .black;

self.modalShadowOffset = keyframeCurrent?.modalShadowOffset
?? keyframePrev?.modalShadowOffset
?? .zero;

self.modalShadowOpacity = keyframeCurrent?.modalShadowOpacity
?? keyframePrev?.modalShadowOpacity
?? 0;

self.modalShadowRadius = keyframeCurrent?.modalShadowRadius
?? keyframePrev?.modalShadowRadius
?? 0;

self.modalCornerRadius = keyframeCurrent?.modalCornerRadius
?? keyframePrev?.modalCornerRadius
Expand Down Expand Up @@ -196,88 +325,11 @@ struct AdaptiveModalInterpolationPoint: Equatable {
?? keyframePrev?.backgroundVisualEffectIntensity
?? (isFirstSnapPoint ? 0 : 1);
};

// MARK: - Functions
// -----------------

func getModalTransform(
shouldApplyRotation: Bool = true,
shouldApplyScale: Bool = true,
shouldApplyTranslate: Bool = true
) -> CGAffineTransform {

var transforms: [CGAffineTransform] = [];

if shouldApplyRotation,
self.modalRotation != 0 {

transforms.append(
.init(rotationAngle: self.modalRotation)
);
};

if shouldApplyScale,
self.modalScaleX != 1 && self.modalScaleY != 1 {

transforms.append(
.init(scaleX: self.modalScaleX, y: self.modalScaleY)
);
};

if shouldApplyTranslate,
self.modalTranslateX != 0 && self.modalTranslateY != 0 {

transforms.append(
.init(translationX: self.modalTranslateX, y: self.modalTranslateY)
);
};

if transforms.isEmpty {
return .identity;
};

return transforms.reduce(.identity){
$0.concatenating($1);
};
};

func apply(toModalView modalView: UIView){
modalView.alpha = self.modalOpacity;

modalView.layer.cornerRadius = self.modalCornerRadius;
modalView.layer.maskedCorners = self.modalMaskedCorners;
};

func apply(toModalWrapperView modalWrapperView: UIView){
modalWrapperView.frame = self.computedRect;
};

func apply(toModalWrapperTransformView view: UIView?){
view?.transform = self.modalTransform;
};

func apply(toDummyModalView dummyModalView: UIView){
dummyModalView.frame = self.computedRect;
};

func apply(toModalBackgroundView modalBgView: UIView?){
modalBgView?.alpha = self.modalBackgroundOpacity;
modalBgView?.backgroundColor = self.modalBackgroundColor;
};

func apply(toModalBackgroundEffectView effectView: UIVisualEffectView?){
effectView?.alpha = self.modalBackgroundVisualEffectOpacity;
};

func apply(toBackgroundView bgView: UIView?){
bgView?.alpha = self.backgroundOpacity;
};

func apply(toBackgroundVisualEffectView effectView: UIView?){
effectView?.alpha = self.backgroundVisualEffectOpacity;
};
};

// MARK: - Helpers
// ---------------

extension AdaptiveModalInterpolationPoint {

static func compute(
Expand Down
Loading

0 comments on commit fe35017

Please sign in to comment.