Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow user to set CropView background color via CropViewConfig #309

Merged
merged 1 commit into from May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions Example/Base.lproj/Main.storyboard
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="20020"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -146,7 +146,6 @@
<state key="normal" title="Dark Background"/>
<connections>
<action selector="darkBackgroundEffect:" destination="BYZ-38-t0r" eventType="touchUpInside" id="edW-tG-CJq"/>
<action selector="noBackgroundEffect:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Chc-Ek-cMW"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="249" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="FmB-6O-5mY">
Expand All @@ -156,16 +155,15 @@
<state key="normal" title="Light Background"/>
<connections>
<action selector="lightBackgroundEffect:" destination="BYZ-38-t0r" eventType="touchUpInside" id="K1m-Uc-kdy"/>
<action selector="noBackgroundEffect:" destination="BYZ-38-t0r" eventType="touchUpInside" id="tYd-3z-IQT"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="249" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="iaH-5e-UFH">
<rect key="frame" x="207.5" y="0.0" width="98.5" height="29"/>
<color key="backgroundColor" systemColor="systemGray6Color"/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
<state key="normal" title="No Background"/>
<state key="normal" title="Color Background"/>
<connections>
<action selector="noBackgroundEffect:" destination="BYZ-38-t0r" eventType="touchUpInside" id="7oq-dx-dxn"/>
<action selector="customColorBackgroundEffect:" destination="BYZ-38-t0r" eventType="touchUpInside" id="2nA-kh-Ri9"/>
</connections>
</button>
</subviews>
Expand Down
15 changes: 13 additions & 2 deletions Example/ViewController.swift
Expand Up @@ -198,8 +198,19 @@ class ViewController: UIViewController, CropViewControllerDelegate {
presentWith(backgroundEffect: .light)
}

@IBAction func noBackgroundEffect(_ sender: Any) {
presentWith(backgroundEffect: .none)
@IBAction func customColorBackgroundEffect(_ sender: Any) {
guard let image = image else {
return
}

var config = Mantis.Config()
config.cropViewConfig.backgroundColor = .yellow
let cropViewController = Mantis.cropViewController(image: image,
config: config)
cropViewController.modalPresentationStyle = .fullScreen
cropViewController.delegate = self
present(cropViewController, animated: true)

}

typealias CropShapeItem = (type: Mantis.CropShapeType, title: String)
Expand Down
4 changes: 4 additions & 0 deletions Sources/Mantis/CropView/CropView.swift
Expand Up @@ -106,6 +106,10 @@ class CropView: UIView {
self.cropMaskViewManager = cropMaskViewManager

super.init(frame: .zero)

if let color = cropViewConfig.backgroundColor {
self.backgroundColor = color
}

viewModel.statusChanged = { [weak self] status in
self?.render(by: status)
Expand Down
9 changes: 9 additions & 0 deletions Sources/Mantis/CropViewConfig.swift
Expand Up @@ -20,6 +20,15 @@ public struct CropViewConfig {

public var cropBorderColor: UIColor = .clear

/**
When backgroundColor is set, cropMaskVisualEffectType is automatically set to custom type
*/
public var backgroundColor: UIColor? {
didSet {
cropMaskVisualEffectType = .custom(color: backgroundColor!)
}
}

public var cropMaskVisualEffectType: CropMaskVisualEffectType = .blurDark

public var presetTransformationType: PresetTransformationType = .none
Expand Down
3 changes: 2 additions & 1 deletion Sources/Mantis/Enum.swift
Expand Up @@ -41,7 +41,8 @@ public enum CropMaskVisualEffectType {
case blurDark
case dark
case light
case none
case custom(color: UIColor)
case `default`
}

public enum CropShapeType: Hashable {
Expand Down
6 changes: 6 additions & 0 deletions Sources/Mantis/Mantis.swift
Expand Up @@ -119,6 +119,12 @@ private func buildCropMaskViewManager(with cropViewConfig: CropViewConfig) -> Cr
let dimmingView = CropDimmingView(cropShapeType: cropViewConfig.cropShapeType)
let visualEffectView = CropMaskVisualEffectView(cropShapeType: cropViewConfig.cropShapeType,
effectType: cropViewConfig.cropMaskVisualEffectType)

if let color = cropViewConfig.backgroundColor {
dimmingView.overLayerFillColor = color.cgColor
visualEffectView.overLayerFillColor = color.cgColor
}

return CropMaskViewManager(dimmingView: dimmingView, visualEffectView: visualEffectView)
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Mantis/MaskBackground/CropDimmingView.swift
Expand Up @@ -9,6 +9,8 @@
import UIKit

class CropDimmingView: UIView, CropMaskProtocol {
var overLayerFillColor: CGColor = UIColor.black.cgColor

var innerLayer: CALayer?

var cropShapeType: CropShapeType = .rect
Expand Down
3 changes: 2 additions & 1 deletion Sources/Mantis/MaskBackground/CropMaskProtocal.swift
Expand Up @@ -14,6 +14,7 @@ private let initialFrameLength: CGFloat = 1000
protocol CropMaskProtocol: UIView {
var cropShapeType: CropShapeType { get set }
var innerLayer: CALayer? { get set }
var overLayerFillColor: CGColor { get set }

func initialize(cropRatio: CGFloat)
func setMask(cropRatio: CGFloat)
Expand Down Expand Up @@ -123,7 +124,7 @@ extension CropMaskProtocol {
let fillLayer = CAShapeLayer()
fillLayer.path = path.cgPath
fillLayer.fillRule = .evenOdd
fillLayer.fillColor = UIColor.black.cgColor
fillLayer.fillColor = overLayerFillColor
fillLayer.opacity = opacity
return fillLayer
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/Mantis/MaskBackground/CropVisualEffectView.swift
Expand Up @@ -9,6 +9,8 @@
import UIKit

class CropMaskVisualEffectView: UIVisualEffectView, CropMaskProtocol {
var overLayerFillColor: CGColor = UIColor.black.cgColor

var innerLayer: CALayer?

var cropShapeType: CropShapeType = .rect
Expand Down Expand Up @@ -47,7 +49,9 @@ class CropMaskVisualEffectView: UIVisualEffectView, CropMaskProtocol {
return (nil, UIColor.black.withAlphaComponent(0.75))
case .light:
return (nil, UIColor.black.withAlphaComponent(0.35))
case .none:
case .custom(let color):
return(nil, color)
case .default:
return (nil, .black)
}
}
Expand Down