From c443695ec27ac8d22557b6160a955b6c4fed1eea Mon Sep 17 00:00:00 2001 From: ezefranca Date: Thu, 20 Sep 2018 16:08:50 -0300 Subject: [PATCH 1/2] covert to swift 4 --- FMBlurable.xcodeproj/project.pbxproj | 46 ++++++++++-- FMBlurable/AppDelegate.swift | 28 +------- FMBlurable/Base.lproj/Main.storyboard | 16 +++-- FMBlurable/ViewController.swift | 94 +++++++++++-------------- FMBlurable/blurable/FMBlurable.swift | 54 +++++++------- FMBlurable/compnents/SliderWidget.swift | 26 ++++--- 6 files changed, 134 insertions(+), 130 deletions(-) diff --git a/FMBlurable.xcodeproj/project.pbxproj b/FMBlurable.xcodeproj/project.pbxproj index 2e6fc18..cab4bc4 100644 --- a/FMBlurable.xcodeproj/project.pbxproj +++ b/FMBlurable.xcodeproj/project.pbxproj @@ -14,9 +14,9 @@ 05C052371BAC9FC700D0108A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 05C052351BAC9FC700D0108A /* LaunchScreen.storyboard */; }; 05C052421BAC9FC700D0108A /* FMBlurableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05C052411BAC9FC700D0108A /* FMBlurableTests.swift */; }; 05C0524D1BAC9FC700D0108A /* FMBlurableUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05C0524C1BAC9FC700D0108A /* FMBlurableUITests.swift */; }; - 05C0525E1BACA49200D0108A /* FMBlurable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05C0525D1BACA49200D0108A /* FMBlurable.swift */; settings = {ASSET_TAGS = (); }; }; - 05EF481A1BB58FE8004EB3D2 /* SliderWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05EF48191BB58FE8004EB3D2 /* SliderWidget.swift */; settings = {ASSET_TAGS = (); }; }; - 05EF481C1BB59201004EB3D2 /* assets in Resources */ = {isa = PBXBuildFile; fileRef = 05EF481B1BB59201004EB3D2 /* assets */; settings = {ASSET_TAGS = (); }; }; + 05C0525E1BACA49200D0108A /* FMBlurable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05C0525D1BACA49200D0108A /* FMBlurable.swift */; }; + 05EF481A1BB58FE8004EB3D2 /* SliderWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05EF48191BB58FE8004EB3D2 /* SliderWidget.swift */; }; + 05EF481C1BB59201004EB3D2 /* assets in Resources */ = {isa = PBXBuildFile; fileRef = 05EF481B1BB59201004EB3D2 /* assets */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -212,11 +212,12 @@ 05C052211BAC9FC700D0108A /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0940; ORGANIZATIONNAME = "Simon Gladman"; TargetAttributes = { 05C052281BAC9FC700D0108A = { CreatedOnToolsVersion = 7.0; + DevelopmentTeam = PB3CY9VB3G; }; 05C0523C1BAC9FC700D0108A = { CreatedOnToolsVersion = 7.0; @@ -347,13 +348,23 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -379,7 +390,8 @@ MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -392,13 +404,23 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -417,6 +439,8 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -426,10 +450,12 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = PB3CY9VB3G; INFOPLIST_FILE = FMBlurable/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.FMBlurable; + PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.Blurable; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -437,10 +463,12 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = PB3CY9VB3G; INFOPLIST_FILE = FMBlurable/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.FMBlurable; + PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.Blurable; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -452,6 +480,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.FMBlurableTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/FMBlurable.app/FMBlurable"; }; name = Debug; @@ -464,6 +493,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.FMBlurableTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/FMBlurable.app/FMBlurable"; }; name = Release; @@ -475,6 +505,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.FMBlurableUITests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; TEST_TARGET_NAME = FMBlurable; USES_XCTRUNNER = YES; }; @@ -487,6 +518,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.FMBlurableUITests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; TEST_TARGET_NAME = FMBlurable; USES_XCTRUNNER = YES; }; diff --git a/FMBlurable/AppDelegate.swift b/FMBlurable/AppDelegate.swift index 443a0e5..f952fc2 100644 --- a/FMBlurable/AppDelegate.swift +++ b/FMBlurable/AppDelegate.swift @@ -13,34 +13,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - // Override point for customization after application launch. + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { return true } - - func applicationWillResignActive(application: UIApplication) { - // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. - // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. - } - - func applicationDidEnterBackground(application: UIApplication) { - // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. - // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. - } - - func applicationWillEnterForeground(application: UIApplication) { - // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. - } - - func applicationDidBecomeActive(application: UIApplication) { - // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. - } - - func applicationWillTerminate(application: UIApplication) { - // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. - } - - } diff --git a/FMBlurable/Base.lproj/Main.storyboard b/FMBlurable/Base.lproj/Main.storyboard index 8c07e5e..e9890bc 100644 --- a/FMBlurable/Base.lproj/Main.storyboard +++ b/FMBlurable/Base.lproj/Main.storyboard @@ -1,7 +1,12 @@ - - + + + + + - + + + @@ -13,10 +18,9 @@ - + - - + diff --git a/FMBlurable/ViewController.swift b/FMBlurable/ViewController.swift index 161c2b4..1d12959 100644 --- a/FMBlurable/ViewController.swift +++ b/FMBlurable/ViewController.swift @@ -18,7 +18,7 @@ class ViewController: UIViewController let slider = UISlider() let stepper = UIStepper() let unarrangedLabel = UILabel() - let unarrangedButton = UIButton(type: UIButtonType.InfoDark) + let unarrangedButton = UIButton(type: UIButtonType.infoDark) let compositeComponent = SliderWidget(title: "Composite Component") let toolbar = UIToolbar() @@ -30,14 +30,14 @@ class ViewController: UIViewController view.addSubview(stackView) view.addSubview(toolbar) - stackView.frame = view.frame.insetBy(dx: 50, dy: 200) + stackView.frame = view.frame.insetBy(dx: 50, dy: 180) - stackView.axis = UILayoutConstraintAxis.Vertical + stackView.axis = UILayoutConstraintAxis.vertical - stackView.alignment = UIStackViewAlignment.Fill - stackView.distribution = UIStackViewDistribution.EqualSpacing + stackView.alignment = UIStackViewAlignment.fill + stackView.distribution = UIStackViewDistribution.equalSpacing - horizontalStackView.distribution = UIStackViewDistribution.FillEqually + horizontalStackView.distribution = UIStackViewDistribution.fillEqually stackView.addArrangedSubview(segmentedControl) stackView.addArrangedSubview(slider) @@ -47,29 +47,29 @@ class ViewController: UIViewController horizontalStackView.addArrangedSubview(stepper) horizontalStackView.addArrangedSubview(toggleSwitch) - unarrangedLabel.text = "Unarranged Label" + unarrangedLabel.text = "label" view.addSubview(unarrangedLabel) - unarrangedButton.setTitle("Unarranged Button", forState: UIControlState.Normal) + unarrangedButton.setTitle("button", for: UIControlState.normal) view.addSubview(unarrangedButton) - let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) + let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) - let toggleSegmentedControl = UIBarButtonItem(title: BarButtonLabel.SegmentedControl.rawValue, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) + let toggleSegmentedControl = UIBarButtonItem(title: BarButtonLabel.segmented.rawValue, style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) - let toggleToggleSwitch = UIBarButtonItem(title: BarButtonLabel.Switch.rawValue, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) + let toggleToggleSwitch = UIBarButtonItem(title: BarButtonLabel.switch.rawValue, style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) - let toggleSlider = UIBarButtonItem(title: BarButtonLabel.Slider.rawValue, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) + let toggleSlider = UIBarButtonItem(title: BarButtonLabel.slider.rawValue, style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) - let toggleStepper = UIBarButtonItem(title: BarButtonLabel.Stepper.rawValue, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) + let toggleStepper = UIBarButtonItem(title: BarButtonLabel.stepper.rawValue, style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) - let toggleUnarrangedLabel = UIBarButtonItem(title: BarButtonLabel.LabelOne.rawValue, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) + let toggleUnarrangedLabel = UIBarButtonItem(title: BarButtonLabel.label.rawValue, style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) - let toggleUnarrangedButton = UIBarButtonItem(title: BarButtonLabel.Button.rawValue, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) + let toggleUnarrangedButton = UIBarButtonItem(title: BarButtonLabel.button.rawValue, style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) - let toggleCompositeComponent = UIBarButtonItem(title: BarButtonLabel.CompositeComponent.rawValue, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) + let toggleCompositeComponent = UIBarButtonItem(title: BarButtonLabel.composite.rawValue, style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.toolbarClickHandler(_:))) toolbar.setItems([toggleSegmentedControl, spacer, toggleToggleSwitch, spacer, @@ -80,10 +80,10 @@ class ViewController: UIViewController toggleCompositeComponent], animated: false) } - func toolbarClickHandler(barButtonItem: UIBarButtonItem) + @objc func toolbarClickHandler(_ barButtonItem: UIBarButtonItem) { guard let title = barButtonItem.title, - barButtonLabel = BarButtonLabel(rawValue: title) else + let barButtonLabel = BarButtonLabel(rawValue: title) else { return } @@ -92,19 +92,19 @@ class ViewController: UIViewController switch barButtonLabel { - case .SegmentedControl: + case .segmented: widget = segmentedControl - case .Switch: + case .switch: widget = toggleSwitch - case .Slider: + case .slider: widget = slider - case .Stepper: + case .stepper: widget = stepper - case .LabelOne: + case .label: widget = unarrangedLabel - case .Button: + case .button: widget = unarrangedButton - case .CompositeComponent: + case .composite: widget = compositeComponent } @@ -114,7 +114,7 @@ class ViewController: UIViewController } else { - widget.blur(blurRadius: 2) + widget.blur(radius: 2) } } @@ -123,41 +123,33 @@ class ViewController: UIViewController stackView.frame = CGRect(x: 0, y: topLayoutGuide.length, width: view.frame.width, - height: view.frame.height - topLayoutGuide.length).insetBy(dx: 50, dy: 50) + height: view.frame.height - topLayoutGuide.length).insetBy(dx: 40, dy: 40) toolbar.frame = CGRect(x: 0, - y: view.frame.height - toolbar.intrinsicContentSize().height, + y: view.frame.height - toolbar.intrinsicContentSize.height - 20, width: view.frame.width, - height: toolbar.intrinsicContentSize().height) + height: toolbar.intrinsicContentSize.height) - unarrangedButton.frame = CGRect(x: view.frame.width - unarrangedButton.intrinsicContentSize().width - 20, - y: 20, - width: unarrangedButton.intrinsicContentSize().width, - height: unarrangedButton.intrinsicContentSize().height) + unarrangedButton.frame = CGRect(x: view.frame.width - unarrangedButton.intrinsicContentSize.width - 40, + y: 40, + width: unarrangedButton.intrinsicContentSize.width, + height: unarrangedButton.intrinsicContentSize.height) unarrangedLabel.frame = CGRect(x: 20, - y: 20, - width: unarrangedLabel.intrinsicContentSize().width, - height: unarrangedLabel.intrinsicContentSize().height) + y: 40, + width: unarrangedLabel.intrinsicContentSize.width, + height: unarrangedLabel.intrinsicContentSize.height) } } enum BarButtonLabel: String { - case SegmentedControl, - Switch, - Slider, - Stepper, - LabelOne, - Button, - CompositeComponent + case segmented, + `switch`, + slider, + stepper, + label, + button, + composite } - - - - - - - - diff --git a/FMBlurable/blurable/FMBlurable.swift b/FMBlurable/blurable/FMBlurable.swift index 3e5fb77..afdd072 100644 --- a/FMBlurable/blurable/FMBlurable.swift +++ b/FMBlurable/blurable/FMBlurable.swift @@ -9,7 +9,6 @@ import UIKit - protocol Blurable { var layer: CALayer { get } @@ -17,10 +16,10 @@ protocol Blurable var frame: CGRect { get } var superview: UIView? { get } - func addSubview(view: UIView) + func add(subview: UIView) func removeFromSuperview() - func blur(blurRadius blurRadius: CGFloat) + func blur(radius: CGFloat) func unBlur() var isBlurred: Bool { get } @@ -28,7 +27,7 @@ protocol Blurable extension Blurable { - func blur(blurRadius blurRadius: CGFloat) + func blur(radius: CGFloat) { if self.superview == nil { @@ -37,54 +36,55 @@ extension Blurable UIGraphicsBeginImageContextWithOptions(CGSize(width: frame.width, height: frame.height), false, 1) - layer.renderInContext(UIGraphicsGetCurrentContext()!) + guard let context = UIGraphicsGetCurrentContext() else { return } + layer.render(in: context) - let image = UIGraphicsGetImageFromCurrentImageContext() + guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return } UIGraphicsEndImageContext(); guard let blur = CIFilter(name: "CIGaussianBlur"), - this = self as? UIView else + let this = self as? UIView else { return } blur.setValue(CIImage(image: image), forKey: kCIInputImageKey) - blur.setValue(blurRadius, forKey: kCIInputRadiusKey) + blur.setValue(radius, forKey: kCIInputRadiusKey) let ciContext = CIContext(options: nil) - let result = blur.valueForKey(kCIOutputImageKey) as! CIImage! + guard let result = blur.value(forKey: kCIOutputImageKey) as? CIImage else { return } let boundingRect = CGRect(x:0, y: 0, width: frame.width, height: frame.height) - let cgImage = ciContext.createCGImage(result, fromRect: boundingRect) + guard let cgImage = ciContext.createCGImage(result, from: boundingRect) else { return } - let filteredImage = UIImage(CGImage: cgImage) + let filteredImage = UIImage(cgImage: cgImage) let blurOverlay = BlurOverlay() blurOverlay.frame = boundingRect blurOverlay.image = filteredImage - blurOverlay.contentMode = UIViewContentMode.Left + blurOverlay.contentMode = UIViewContentMode.left if let superview = superview as? UIStackView, - index = (superview as UIStackView).arrangedSubviews.indexOf(this) + let index = (superview as UIStackView).arrangedSubviews.index(of: this) { removeFromSuperview() - superview.insertArrangedSubview(blurOverlay, atIndex: index) + superview.insertArrangedSubview(blurOverlay, at: index) } else { blurOverlay.frame.origin = frame.origin - UIView.transitionFromView(this, - toView: blurOverlay, + UIView.transition(from: this, + to: blurOverlay, duration: 0.2, - options: UIViewAnimationOptions.CurveEaseIn, + options: UIViewAnimationOptions.curveEaseIn, completion: nil) } @@ -97,25 +97,25 @@ extension Blurable func unBlur() { guard let this = self as? UIView, - blurOverlay = objc_getAssociatedObject(self as? UIView, &BlurableKey.blurable) as? BlurOverlay else + let blurOverlay = objc_getAssociatedObject(self as? UIView ?? UIView(), &BlurableKey.blurable) as? BlurOverlay else { return } if let superview = blurOverlay.superview as? UIStackView, - index = (blurOverlay.superview as! UIStackView).arrangedSubviews.indexOf(blurOverlay) + let index = (blurOverlay.superview as? UIStackView)?.arrangedSubviews.index(of: blurOverlay) { blurOverlay.removeFromSuperview() - superview.insertArrangedSubview(this, atIndex: index) + superview.insertArrangedSubview(this, at: index) } else { this.frame.origin = blurOverlay.frame.origin - UIView.transitionFromView(blurOverlay, - toView: this, + UIView.transition(from: blurOverlay, + to: this, duration: 0.2, - options: UIViewAnimationOptions.CurveEaseIn, + options: UIViewAnimationOptions.curveEaseIn, completion: nil) } @@ -127,12 +127,16 @@ extension Blurable var isBlurred: Bool { - return objc_getAssociatedObject(self as? UIView, &BlurableKey.blurable) is BlurOverlay + return objc_getAssociatedObject(self as? UIView ?? UIView(), &BlurableKey.blurable) is BlurOverlay } } extension UIView: Blurable { + func add(subview: UIView) { + self.addSubview(subview) + } + } class BlurOverlay: UIImageView @@ -142,4 +146,4 @@ class BlurOverlay: UIImageView struct BlurableKey { static var blurable = "blurable" -} \ No newline at end of file +} diff --git a/FMBlurable/compnents/SliderWidget.swift b/FMBlurable/compnents/SliderWidget.swift index 1c96d6d..21fa5b5 100644 --- a/FMBlurable/compnents/SliderWidget.swift +++ b/FMBlurable/compnents/SliderWidget.swift @@ -10,12 +10,12 @@ import UIKit class SliderWidget: UIControl { - let slider = UISlider(frame: CGRectZero) - let label = UILabel(frame: CGRectZero) + let slider = UISlider(frame: CGRect.zero) + let label = UILabel(frame: CGRect.zero) required init(title: String) { - super.init(frame: CGRectZero) + super.init(frame: CGRect.zero) self.title = title @@ -46,22 +46,21 @@ class SliderWidget: UIControl override func didMoveToSuperview() { - slider.addTarget(self, action: #selector(SliderWidget.sliderChangeHandler), forControlEvents: .ValueChanged) + slider.addTarget(self, action: #selector(SliderWidget.sliderChangeHandler), for: .valueChanged) layer.cornerRadius = 5 - layer.borderColor = UIColor.whiteColor().CGColor + layer.borderColor = UIColor.white.cgColor layer.borderWidth = 2 - layer.backgroundColor = UIColor.darkGrayColor().colorWithAlphaComponent(0.25).CGColor + layer.backgroundColor = UIColor.darkGray.withAlphaComponent(0.25).cgColor - addSubview(slider) - addSubview(label) + add(subview: slider) + add(subview: label) } - func sliderChangeHandler() + @objc func sliderChangeHandler() { value = slider.value - - sendActionsForControlEvents(.ValueChanged) + sendActions(for: .valueChanged) } func updateLabel() @@ -75,8 +74,7 @@ class SliderWidget: UIControl slider.frame = CGRect(x: 0, y: frame.height / 2, width: frame.width, height: frame.height / 2).insetBy(dx: 5, dy: 5) } - override func intrinsicContentSize() -> CGSize - { - return CGSize(width: 640, height: 75) + override var intrinsicContentSize: CGSize { + return CGSize(width: 640, height: 200) } } From f6c6a8c6768a3fb8b0382fbae6686e237e96a2b3 Mon Sep 17 00:00:00 2001 From: ezefranca Date: Thu, 20 Sep 2018 16:10:26 -0300 Subject: [PATCH 2/2] fix readme --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4a6238a..d425bd0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Blurable -###Apply a Gaussian Blur to any UIView with Swift Protocol Extensions +### Apply a Gaussian Blur to any UIView with Swift Protocol Extensions -##### _Adds `blur()` and `unBlur()` methods to `UIView` components which applies a Core Image Gaussian blur filter to the contents._ +### _Adds `blur()` and `unBlur()` methods to `UIView` components which applies a Core Image Gaussian blur filter to the contents._ ######_Companion project to this blog post: http://flexmonkey.blogspot.co.uk/2015/09/applying-gaussian-blur-to-uiviews-with.html_ @@ -46,7 +46,7 @@ Obviously, just being a protocol, it doesn't do much on its own. However, by add 1. Download and drop ```FMBlurable.swift``` in your project. 2. Congratulations! -##The Mechanics of Blurable +## The Mechanics of Blurable Getting a blurred representation of a `UIView` is pretty simple: I need to begin an image context, use the view's layer's renderInContext method to render into the context and then get a UIImage from the context: @@ -171,7 +171,7 @@ Finally, to see if a `UIView` is currently blurred, I created `isBlurred()` whic } ``` -##Blurring a UIView +## Blurring a UIView To blur and de-blur, just invoke `blur()` and `unBlur()` on an UIView: @@ -180,6 +180,6 @@ To blur and de-blur, just invoke `blur()` and `unBlur()` on an UIView: segmentedControl.blur(blurRadius: 2) ``` -##Source Code +## Source Code As always, the source code for this project is available at my GitHub repository here. Enjoy!