Skip to content

Commit

Permalink
fix: update setters to change blur properties in the UIView subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Jun 1, 2023
1 parent f42f12a commit 311f672
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 41 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ import { BlurViewView } from '@candlefinance/blur-view';
/>;
```

## Docs

View the example app in the [example](./example/src/App.tsx) folder.

| Property | Type | Default | Description |
| --------------- | -------- | --------------------------------------- | ------------------------------------------------- |
| `maxBlurRadius` | `number` | `20` | The amount of blur to apply to the view. |
| `gradientMask` | `string` | see [source](./ios/images/Gradient.png) | The gradient mask to apply to the view in base64. |

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- boost (1.76.0)
- candlefinance-blur-view (0.1.0):
- candlefinance-blur-view (0.3.0):
- React-Core
- CocoaAsyncSocket (7.6.5)
- DoubleConversion (1.1.6)
Expand Down Expand Up @@ -574,7 +574,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
boost: 57d2868c099736d80fcd648bf211b4431e51a558
candlefinance-blur-view: 7a7baae3a3c8cd4c84c6973e6c3cff7bd2c87d4f
candlefinance-blur-view: 0818b16cefbe918e533fbdd78771c92764a2a0e2
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
FBLazyVector: f637f31eacba90d4fdeff3fa41608b8f361c173b
Expand Down
15 changes: 8 additions & 7 deletions ios/BlurViewViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,33 @@ class BlurViewViewManager: RCTViewManager {
class BlurViewView : UIView {

var gradientMaskImage: UIImage = VariableBlurViewConstants.defaultGradientMask
var _maxBlurRadius: CGFloat = 20

@objc var filterType: String = "variableBlur"
private let filterType: String = "variableBlur"
@objc var gradientMask: String = "" {
didSet {
if
let data = Data(base64Encoded: gradientMask, options: .ignoreUnknownCharacters),
let image = UIImage(data: data)
{
gradientMaskImage = image
self.gradientMaskImage = image
blurView.update(gradientMask: image, maxBlurRadius: CGFloat(truncating: maxBlurRadius), filterType: filterType)
} else {
print("[VariableBlurView] Couldn't create the gradient mask image.")
gradientMaskImage = UIImage(systemName: "xmark")!
let image = UIImage(systemName: "xmark")!
blurView.update(gradientMask: image, maxBlurRadius: CGFloat(truncating: maxBlurRadius), filterType: filterType)
}
}
}

@objc var maxBlurRadius: NSNumber = 20 {
didSet {
self._maxBlurRadius = CGFloat(truncating: maxBlurRadius)
blurView.update(gradientMask: gradientMaskImage, maxBlurRadius: maxBlurRadius.doubleValue, filterType: filterType)
}
}

lazy var blurView = VariableBlurUIView(
gradientMask: gradientMaskImage,
maxBlurRadius: _maxBlurRadius,
gradientMask: VariableBlurViewConstants.defaultGradientMask,
maxBlurRadius: maxBlurRadius.doubleValue,
filterType: filterType
)

Expand Down
41 changes: 9 additions & 32 deletions ios/VariableFilterBlurView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import SwiftUI

/// A variable blur view.
public class VariableBlurUIView: UIVisualEffectView {
public init(
Expand All @@ -39,6 +37,14 @@ public class VariableBlurUIView: UIVisualEffectView {
) {
super.init(effect: UIBlurEffect(style: .regular))

update(gradientMask: gradientMask, maxBlurRadius: maxBlurRadius, filterType: filterType)
}

public func update(
gradientMask: UIImage,
maxBlurRadius: CGFloat = 20,
filterType: String = "variableBlur"
) {
/// This is a private QuartzCore class, encoded in base64.
///
/// CAFilter
Expand Down Expand Up @@ -129,35 +135,6 @@ public class VariableBlurUIView: UIVisualEffectView {
}
}

/// A variable blur view.
public struct VariableBlurView: UIViewRepresentable {
public var gradientMask: UIImage
public var maxBlurRadius: CGFloat
public var filterType: String

/// A variable blur view.
public init(
gradientMask: UIImage = VariableBlurViewConstants.defaultGradientMask,
maxBlurRadius: CGFloat = 20,
filterType: String = "variableBlur"
) {
self.gradientMask = gradientMask
self.maxBlurRadius = maxBlurRadius
self.filterType = filterType
}

public func makeUIView(context: Context) -> VariableBlurUIView {
let view = VariableBlurUIView(
gradientMask: gradientMask,
maxBlurRadius: maxBlurRadius,
filterType: filterType
)
return view
}

public func updateUIView(_ uiView: VariableBlurUIView, context: Context) {}
}

public enum VariableBlurViewConstants {

/// A gradient mask image (top is opaque, bottom is clear). The gradient includes easing.
Expand Down Expand Up @@ -332,4 +309,4 @@ public enum VariableBlurViewConstants {
hRk7ZiOn82b8tBT4z9WYYimWfkLE9NPSNSC9M8IteGAx2NVHvDRQaepzekklf1xAyM26roGzLps3
bjvf9+1Pfl3/GTX2ayFpmkPjAAAAAElFTkSuQmCC
"""
}
}

0 comments on commit 311f672

Please sign in to comment.