Skip to content

Commit 6645ad5

Browse files
author
Pierre
committed
Update for swift 4.2 -- Fix thread issues
1 parent 4e741e7 commit 6645ad5

File tree

9 files changed

+80
-134
lines changed

9 files changed

+80
-134
lines changed

ShadowView/ShadowView.xcodeproj/project.pbxproj

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@
260260
};
261261
F907D1191F27ECA3007D4BE4 = {
262262
CreatedOnToolsVersion = 8.3.3;
263-
DevelopmentTeam = PCKYQNTV3N;
264-
LastSwiftMigration = 0830;
263+
DevelopmentTeam = P5PSPS572B;
264+
LastSwiftMigration = 1000;
265265
ProvisioningStyle = Automatic;
266266
};
267267
};
@@ -561,14 +561,15 @@
561561
buildSettings = {
562562
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
563563
CLANG_ENABLE_MODULES = YES;
564-
DEVELOPMENT_TEAM = PCKYQNTV3N;
564+
DEVELOPMENT_TEAM = P5PSPS572B;
565565
INFOPLIST_FILE = ShadowViewExample/Info.plist;
566566
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
567567
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
568-
PRODUCT_BUNDLE_IDENTIFIER = PP.ShadowViewExample;
568+
PRODUCT_BUNDLE_IDENTIFIER = PP.ShadowViewExample.Pierre.Perrin;
569569
PRODUCT_NAME = "$(TARGET_NAME)";
570570
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
571-
SWIFT_VERSION = 3.0;
571+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
572+
SWIFT_VERSION = 4.2;
572573
};
573574
name = Debug;
574575
};
@@ -577,13 +578,14 @@
577578
buildSettings = {
578579
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
579580
CLANG_ENABLE_MODULES = YES;
580-
DEVELOPMENT_TEAM = PCKYQNTV3N;
581+
DEVELOPMENT_TEAM = P5PSPS572B;
581582
INFOPLIST_FILE = ShadowViewExample/Info.plist;
582583
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
583584
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
584-
PRODUCT_BUNDLE_IDENTIFIER = PP.ShadowViewExample;
585+
PRODUCT_BUNDLE_IDENTIFIER = PP.ShadowViewExample.Pierre.Perrin;
585586
PRODUCT_NAME = "$(TARGET_NAME)";
586-
SWIFT_VERSION = 3.0;
587+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
588+
SWIFT_VERSION = 4.2;
587589
};
588590
name = Release;
589591
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

ShadowView/ShadowView/ShadowView+BlurProcess.swift

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,47 @@ extension ShadowView{
2626

2727

2828

29-
public func updateShadow(){
29+
public func updateShadow(dispatchQueue: DispatchQueue = .main){
3030

3131
self.shadowImageView.image = nil
32-
// DispatchQueue.global(qos: DispatchQoS.QoSClass.utility).async { [weak self] in
33-
self.createLayerImage()
34-
// }
32+
self.createLayerImage(dispatchQueue: dispatchQueue)
3533
}
3634

37-
private func createLayerImage(){
35+
private func createLayerImage(dispatchQueue: DispatchQueue = .main){
3836

39-
self.convertToImage { (image) in
40-
41-
let containerLayer = CALayer()
42-
let imageSize = image.size
43-
containerLayer.frame = CGRect(origin: .zero, size: imageSize.scaled(by:self.scaleImageConstant))
44-
containerLayer.backgroundColor = UIColor.clear.cgColor
45-
let blurImageLayer = CALayer()
46-
blurImageLayer.frame = CGRect(origin: .zero,size: imageSize)
47-
blurImageLayer.position = CGPoint(x:containerLayer.bounds.midX,y:containerLayer.bounds.midY)
48-
blurImageLayer.contents = image.applyBlurWithRadius(0, tintColor:self.shadowColor, saturationDeltaFactor: self.shadowSaturation)?.cgImage
49-
50-
blurImageLayer.masksToBounds = false
51-
containerLayer.addSublayer(blurImageLayer)
52-
53-
let containerImage = containerLayer.asImage
54-
55-
56-
let resizeImageConstant :CGFloat = self.highPerformanceBlur ? 0.3 : 1
57-
guard let resizedContainerImage = containerImage.resized(withPercentage: resizeImageConstant),
58-
let blurredImage = resizedContainerImage.applyBlur(blurRadius: self.blurRadius)
59-
else {
60-
return
61-
}
62-
6337
DispatchQueue.main.async { [weak self] in
64-
self?.layer.masksToBounds = false
65-
self?.shadowImageView?.image = blurredImage
38+
39+
guard let image = self?.asImage else { return }
40+
41+
let shadowColor = self?.shadowColor ?? .clear
42+
let shadowSaturation = self?.shadowSaturation ?? 1
43+
let scaleImageConstant = self?.scaleImageConstant ?? 1
44+
let blurRadius = self?.blurRadius ?? 20
45+
dispatchQueue.async { [weak self] in
46+
47+
let containerLayer = CALayer()
48+
let imageSize = image.size
49+
containerLayer.frame = CGRect(origin: .zero, size: imageSize.scaled(by:scaleImageConstant))
50+
containerLayer.backgroundColor = UIColor.clear.cgColor
51+
let blurImageLayer = CALayer()
52+
blurImageLayer.frame = CGRect(origin: .zero,size: imageSize)
53+
blurImageLayer.position = CGPoint(x:containerLayer.bounds.midX,y:containerLayer.bounds.midY)
54+
blurImageLayer.contents = image.applyBlurWithRadius(0, tintColor: shadowColor, saturationDeltaFactor: shadowSaturation)?.cgImage
55+
56+
blurImageLayer.masksToBounds = false
57+
containerLayer.addSublayer(blurImageLayer)
58+
let containerImage = containerLayer.asImage
59+
60+
61+
let resizeImageConstant :CGFloat = 1
62+
guard let resizedContainerImage = containerImage.resized(withPercentage: resizeImageConstant),
63+
let blurredImage = resizedContainerImage.applyBlur(blurRadius: blurRadius)
64+
else { return }
65+
66+
DispatchQueue.main.async { [weak self] in
67+
self?.layer.masksToBounds = false
68+
self?.shadowImageView?.image = blurredImage
69+
}
6670
}
6771
}
6872

ShadowView/ShadowView/ShadowView.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ public class ShadowView: UIView {
1313
internal var blurRadius :CGFloat = 5.0
1414
public var shadowImageView : UIImageView!
1515
internal let scaleImageConstant :CGFloat = 3
16-
17-
public var highPerformanceBlur = true
18-
1916
public var correctShadowScale : CGFloat{
2017
return shadowScale + scaleImageConstant - 1
2118
}
@@ -42,21 +39,22 @@ public class ShadowView: UIView {
4239
}
4340
}
4441
private var shadowTintColor : UIColor?
45-
public override var shadowColor: UIColor?{
42+
43+
@IBInspectable public var shadowColor: UIColor?{
4644
get{
4745
return shadowTintColor
4846
}set{
4947
shadowTintColor = newValue
5048
}
5149
}
5250

53-
public override var shadowOffset : CGSize{
51+
@IBInspectable public var shadowOffset : CGSize = .zero{
5452
didSet{
5553
layoutSubviews()
5654
}
5755
}
5856

59-
public override var shadowRadius : CGFloat{
57+
@IBInspectable public var shadowRadius : CGFloat{
6058
set{
6159
blurRadius = newValue
6260
updateShadow()
@@ -66,7 +64,7 @@ public class ShadowView: UIView {
6664
}
6765
}
6866

69-
public override var shadowOpacity: Float{
67+
@IBInspectable public var shadowOpacity: Float{
7068
set{
7169
shadowImageView.alpha = CGFloat(newValue)
7270
}

ShadowView/ShadowView/UIView+Image.swift

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,23 @@ import UIKit
1111
extension UIView{
1212

1313
///Returns a UIImage copy of the view
14-
internal func convertToImage(completion:@escaping (UIImage) -> Void){
15-
16-
DispatchQueue.main.async {
17-
let layer = self.layer
18-
let size = layer.frame.size
19-
20-
DispatchQueue.global(qos: .utility).async {
21-
let image = layer.getImage(size: size)
22-
completion(image)
23-
}
24-
}
14+
internal var asImage : UIImage{
15+
return layer.asImage
2516
}
2617
}
2718

2819
extension CALayer{
2920

30-
func getImage(size:CGSize) -> UIImage{
31-
32-
UIGraphicsBeginImageContext(size)
33-
guard let currentContext = UIGraphicsGetCurrentContext()else{return UIImage()}
34-
render(in:currentContext)
35-
let image = UIGraphicsGetImageFromCurrentImageContext()
36-
UIGraphicsEndImageContext()
37-
38-
guard let cgImage = image?.cgImage else{return UIImage()}
39-
return UIImage.init(cgImage: cgImage)
40-
}
41-
4221
///Returns a UIImage copy of the layer
4322
internal var asImage : UIImage{
44-
45-
return self.getImage(size: self.frame.size)
23+
UIGraphicsBeginImageContext(frame.size)
24+
render(in: UIGraphicsGetCurrentContext()!)
25+
let image = UIGraphicsGetImageFromCurrentImageContext()
26+
UIGraphicsEndImageContext()
27+
if let cgImage = image?.cgImage {
28+
return UIImage(cgImage: cgImage)
29+
} else {
30+
return UIImage()
31+
}
4632
}
4733
}

ShadowView/ShadowView/UIView+Shadow.swift

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,3 @@
77
//
88

99
import UIKit
10-
11-
extension UIView{
12-
13-
@IBInspectable public var shadowRadius : CGFloat{
14-
set{
15-
layer.shadowRadius = newValue
16-
layer.masksToBounds = newValue < 0
17-
}
18-
get{
19-
return layer.shadowRadius
20-
}
21-
}
22-
23-
@IBInspectable public var shadowOffset : CGSize{
24-
set{
25-
layer.shadowOffset = newValue
26-
}
27-
get{
28-
return layer.shadowOffset
29-
}
30-
}
31-
32-
33-
@IBInspectable public var shadowColor : UIColor?{
34-
set{
35-
layer.shadowColor = newValue?.cgColor
36-
}
37-
get{
38-
return UIColor(cgColor: layer.shadowColor ?? UIColor().cgColor)
39-
}
40-
}
41-
42-
@IBInspectable public var shadowOpacity : Float{
43-
set{
44-
layer.shadowOpacity = newValue
45-
layer.masksToBounds = newValue < 0
46-
}
47-
get{
48-
return layer.shadowOpacity
49-
}
50-
}
51-
52-
@IBInspectable public var shadowPath : CGPath?{
53-
set{
54-
layer.shadowPath = newValue
55-
}
56-
get{
57-
return layer.shadowPath
58-
}
59-
}
60-
}

ShadowView/ShadowViewExample/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1414
var window: UIWindow?
1515

1616

17-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
17+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1818
// Override point for customization after application launch.
1919
return true
2020
}

ShadowView/ShadowViewExample/Base.lproj/Main.storyboard

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="2yo-QA-Q5K">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="2yo-QA-Q5K">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
9-
<capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
109
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1110
</dependencies>
1211
<scenes>
@@ -32,7 +31,7 @@
3231
<rect key="frame" x="0.0" y="0.0" width="163.5" height="163.5"/>
3332
<subviews>
3433
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="sample.jpg" translatesAutoresizingMaskIntoConstraints="NO" id="vHf-yD-evt">
35-
<rect key="frame" x="0.0" y="0.0" width="164" height="164"/>
34+
<rect key="frame" x="0.0" y="0.0" width="163.5" height="163.5"/>
3635
</imageView>
3736
</subviews>
3837
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
@@ -64,16 +63,16 @@
6463
<constraint firstAttribute="width" secondItem="lUd-IE-JvU" secondAttribute="height" id="uvx-hC-qwD"/>
6564
</constraints>
6665
<userDefinedRuntimeAttributes>
67-
<userDefinedRuntimeAttribute type="number" keyPath="shadowRadius">
66+
<userDefinedRuntimeAttribute type="number" keyPath="layer.shadowRadius">
6867
<real key="value" value="5"/>
6968
</userDefinedRuntimeAttribute>
70-
<userDefinedRuntimeAttribute type="size" keyPath="shadowOffset">
69+
<userDefinedRuntimeAttribute type="size" keyPath="layer.shadowOffset">
7170
<size key="value" width="0.0" height="5"/>
7271
</userDefinedRuntimeAttribute>
73-
<userDefinedRuntimeAttribute type="number" keyPath="shadowOpacity">
72+
<userDefinedRuntimeAttribute type="number" keyPath="layer.shadowOpacity">
7473
<real key="value" value="0.69999999999999996"/>
7574
</userDefinedRuntimeAttribute>
76-
<userDefinedRuntimeAttribute type="color" keyPath="shadowColor">
75+
<userDefinedRuntimeAttribute type="color" keyPath="layer.shadowColor">
7776
<color key="value" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
7877
</userDefinedRuntimeAttribute>
7978
</userDefinedRuntimeAttributes>
@@ -198,7 +197,6 @@
198197
<!--Collection View Example Collection View Controller-->
199198
<scene sceneID="Sk3-uc-K7P">
200199
<objects>
201-
<placeholder placeholderIdentifier="IBFirstResponder" id="uYJ-9c-ZML" userLabel="First Responder" sceneMemberID="firstResponder"/>
202200
<collectionViewController id="C4x-Q2-0J5" customClass="CollectionViewExampleCollectionViewController" customModule="ShadowViewExample" customModuleProvider="target" sceneMemberID="viewController">
203201
<collectionView key="view" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" id="g2o-qQ-xNu">
204202
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
@@ -253,6 +251,7 @@
253251
</connections>
254252
</collectionView>
255253
</collectionViewController>
254+
<placeholder placeholderIdentifier="IBFirstResponder" id="uYJ-9c-ZML" userLabel="First Responder" sceneMemberID="firstResponder"/>
256255
</objects>
257256
<point key="canvasLocation" x="2432.8000000000002" y="35.532233883058474"/>
258257
</scene>

ShadowView/ShadowViewExample/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ViewController: UIViewController {
2727
sender.isContinuous = false
2828

2929
self.exampleView.shadowRadius = CGFloat(sender.value * 50)
30-
self.otherImage.shadowRadius = CGFloat(sender.value * 50)
30+
self.otherImage.layer.shadowRadius = CGFloat(sender.value * 50)
3131
}
3232

3333
@IBAction func shadowSize(_ sender: UISlider) {
@@ -38,7 +38,7 @@ class ViewController: UIViewController {
3838
@IBAction func shadowOpacity(_ sender: UISlider) {
3939

4040
self.exampleView.shadowOpacity = sender.value
41-
self.otherImage.shadowOpacity = sender.value
41+
self.otherImage.layer.shadowOpacity = sender.value
4242
}
4343

4444
@IBAction func CHANGEiMAGE(_ sender: Any) {

0 commit comments

Comments
 (0)