diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index c9b55ad..a2805c4 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1,6 +1,6 @@ PODS: - boost (1.76.0) - - candlefinance-blur-view (0.3.1): + - candlefinance-blur-view (0.4.0): - React-Core - VisualEffectView - CocoaAsyncSocket (7.6.5) @@ -609,7 +609,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: boost: 57d2868c099736d80fcd648bf211b4431e51a558 - candlefinance-blur-view: 81152897d8e2453946000f809378f45a63d5a682 + candlefinance-blur-view: c33b213c7e7f3d116206a31e9057ae79fca4db09 CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99 DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54 FBLazyVector: f637f31eacba90d4fdeff3fa41608b8f361c173b diff --git a/ios/BlurViewViewManager.swift b/ios/BlurViewViewManager.swift index 3558a66..34806c8 100644 --- a/ios/BlurViewViewManager.swift +++ b/ios/BlurViewViewManager.swift @@ -16,17 +16,20 @@ final class BlurViewView : UIView { lazy var blurView = VisualEffectView() - @objc var blurTintColor: String? = nil { + @objc var blurTintColor: NSString? = nil { didSet { - if let blurTintColor { - blurView.colorTint = UIColor(hex: blurTintColor) + if let blurTintColor, + let color = UIColor(hexString: blurTintColor as String) { + blurView.colorTint = color + } else { + print("Error: could not set color \(blurTintColor)") } } } @objc var blurRadius: NSNumber = 0 { didSet { - blurView.blurRadius = blurRadius.doubleValue + blurView.blurRadius = CGFloat(blurRadius.doubleValue) } } @@ -38,7 +41,8 @@ final class BlurViewView : UIView { @objc var colorTintOpacity: NSNumber = 0 { didSet { - blurView.colorTintAlpha = colorTintOpacity.doubleValue + print("set color", colorTintOpacity.doubleValue) + blurView.colorTintAlpha = CGFloat(colorTintOpacity.doubleValue) } } @@ -52,6 +56,7 @@ final class BlurViewView : UIView { super.init(frame: CGRect.zero) addSubview(blurView) + blurView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ @@ -68,30 +73,133 @@ final class BlurViewView : UIView { } -private extension UIColor { - convenience init?(hex: String) { - let r, g, b, a: CGFloat - +#if os(iOS) || os(tvOS) || os(watchOS) + import UIKit + typealias SWColor = UIColor +#else + import Cocoa + typealias SWColor = NSColor +#endif + +private extension Int64 { + func duplicate4bits() -> Int64 { + return (self << 4) + self + } +} + +/// An extension of UIColor (on iOS) or NSColor (on OSX) providing HEX color handling. +public extension SWColor { + private convenience init?(hex3: Int64, alpha: Float) { + self.init(red: CGFloat( ((hex3 & 0xF00) >> 8).duplicate4bits() ) / 255.0, + green: CGFloat( ((hex3 & 0x0F0) >> 4).duplicate4bits() ) / 255.0, + blue: CGFloat( ((hex3 & 0x00F) >> 0).duplicate4bits() ) / 255.0, + alpha: CGFloat(alpha)) + } + + private convenience init?(hex4: Int64, alpha: Float?) { + self.init(red: CGFloat( ((hex4 & 0xF000) >> 12).duplicate4bits() ) / 255.0, + green: CGFloat( ((hex4 & 0x0F00) >> 8).duplicate4bits() ) / 255.0, + blue: CGFloat( ((hex4 & 0x00F0) >> 4).duplicate4bits() ) / 255.0, + alpha: alpha.map(CGFloat.init(_:)) ?? CGFloat( ((hex4 & 0x000F) >> 0).duplicate4bits() ) / 255.0) + } + + private convenience init?(hex6: Int64, alpha: Float) { + self.init(red: CGFloat( (hex6 & 0xFF0000) >> 16 ) / 255.0, + green: CGFloat( (hex6 & 0x00FF00) >> 8 ) / 255.0, + blue: CGFloat( (hex6 & 0x0000FF) >> 0 ) / 255.0, alpha: CGFloat(alpha)) + } + + private convenience init?(hex8: Int64, alpha: Float?) { + self.init(red: CGFloat( (hex8 & 0xFF000000) >> 24 ) / 255.0, + green: CGFloat( (hex8 & 0x00FF0000) >> 16 ) / 255.0, + blue: CGFloat( (hex8 & 0x0000FF00) >> 8 ) / 255.0, + alpha: alpha.map(CGFloat.init(_:)) ?? CGFloat( (hex8 & 0x000000FF) >> 0 ) / 255.0) + } + + /** + Create non-autoreleased color with in the given hex string and alpha. + + - parameter hexString: The hex string, with or without the hash character. + - parameter alpha: The alpha value, a floating value between 0 and 1. + - returns: A color with the given hex string and alpha. + */ + convenience init?(hexString: String, alpha: Float? = nil) { + var hex = hexString + + // Check for hash and remove the hash if hex.hasPrefix("#") { - let start = hex.index(hex.startIndex, offsetBy: 1) - let hexColor = String(hex[start...]) - - if hexColor.count == 8 { - let scanner = Scanner(string: hexColor) - var hexNumber: UInt64 = 0 - - if scanner.scanHexInt64(&hexNumber) { - r = CGFloat((hexNumber & 0xff000000) >> 24) / 255 - g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255 - b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255 - a = CGFloat(hexNumber & 0x000000ff) / 255 - - self.init(red: r, green: g, blue: b, alpha: a) - return - } - } + hex = String(hex[hex.index(after: hex.startIndex)...]) + } + + guard let hexVal = Int64(hex, radix: 16) else { + self.init() + return nil + } + + switch hex.count { + case 3: + self.init(hex3: hexVal, alpha: alpha ?? 1.0) + case 4: + self.init(hex4: hexVal, alpha: alpha) + case 6: + self.init(hex6: hexVal, alpha: alpha ?? 1.0) + case 8: + self.init(hex8: hexVal, alpha: alpha) + default: + // Note: + // The swift 1.1 compiler is currently unable to destroy partially initialized classes in all cases, + // so it disallows formation of a situation where it would have to. We consider this a bug to be fixed + // in future releases, not a feature. -- Apple Forum + self.init() + return nil + } + } + + /** + Create non-autoreleased color with in the given hex value and alpha + + - parameter hex: The hex value. For example: 0xff8942 (no quotation). + - parameter alpha: The alpha value, a floating value between 0 and 1. + - returns: color with the given hex value and alpha + */ + convenience init?(hex: Int, alpha: Float = 1.0) { + if (0x000000 ... 0xFFFFFF) ~= hex { + self.init(hex6: Int64(hex), alpha: alpha) + } else { + self.init() + return nil + } + } + + convenience init?(argbHex: Int) { + if (0x00000000 ... 0xFFFFFFFF) ~= argbHex { + let hex = Int64(argbHex) + self.init(red: CGFloat( (hex & 0x00FF0000) >> 16 ) / 255.0, + green: CGFloat( (hex & 0x0000FF00) >> 8 ) / 255.0, + blue: CGFloat( (hex & 0x000000FF) >> 0 ) / 255.0, + alpha: CGFloat( (hex & 0xFF000000) >> 24 ) / 255.0) + } else { + self.init() + return nil + } + } + + convenience init?(argbHexString: String) { + var hex = argbHexString + + // Check for hash and remove the hash + if hex.hasPrefix("#") { + hex = String(hex[hex.index(after: hex.startIndex)...]) } - return nil + guard hex.count == 8, let hexVal = Int64(hex, radix: 16) else { + self.init() + return nil + } + self.init(red: CGFloat( (hexVal & 0x00FF0000) >> 16 ) / 255.0, + green: CGFloat( (hexVal & 0x0000FF00) >> 8 ) / 255.0, + blue: CGFloat( (hexVal & 0x000000FF) >> 0 ) / 255.0, + alpha: CGFloat( (hexVal & 0xFF000000) >> 24 ) / 255.0) } + } diff --git a/src/index.tsx b/src/index.tsx index 51e0650..e8e9ce2 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -7,6 +7,7 @@ type BlurViewProps = { blurEnabled?: boolean; colorTintOpacity?: number; scale?: number; + children?: React.ReactNode; }; const ComponentName = 'BlurViewView';