Skip to content

Commit 35b7374

Browse files
authored
Merge pull request #13 from tijme/master
Fixed view appeared behind the keyboard (#7). Also added a "cross" preset (#9).
2 parents 4faee5e + 40d4a42 commit 35b7374

File tree

6 files changed

+111
-4
lines changed

6 files changed

+111
-4
lines changed

Example/Controllers/ViewController.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ class ViewController: UITableViewController {
55

66
let data: [Alert] = [
77
Alert(key: "Done", preset: .done, title: "Added to Library", subtitle: nil),
8-
Alert(key: "Heart", preset: .heart, title: "Love", subtitle: "We'll recommend more like this in for You"),
9-
Alert(key: "Message", preset: nil, title: nil, subtitle: "Email requerid")
8+
Alert(key: "Heart", preset: .heart, title: "Love", subtitle: "We'll recommend more like this for you"),
9+
Alert(key: "Message", preset: nil, title: nil, subtitle: "Email required"),
10+
Alert(key: "Error", preset: .error, title: "Oops", subtitle: "Please try again later")
1011
]
1112

1213
var selectedIndexPath: IndexPath {

SPAlert.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
706F894A2383277500ECF5D1 /* SPAlertIconErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 706F89492383277500ECF5D1 /* SPAlertIconErrorView.swift */; };
1011
F41204E92382AC9B009C2AC7 /* SPAlertView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F41204DF2382AC9B009C2AC7 /* SPAlertView.swift */; };
1112
F41204EA2382AC9B009C2AC7 /* SPAlertPreset.swift in Sources */ = {isa = PBXBuildFile; fileRef = F41204E02382AC9B009C2AC7 /* SPAlertPreset.swift */; };
1213
F41204EB2382AC9B009C2AC7 /* SPAlertIconHeartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F41204E22382AC9B009C2AC7 /* SPAlertIconHeartView.swift */; };
@@ -49,6 +50,7 @@
4950
/* End PBXCopyFilesBuildPhase section */
5051

5152
/* Begin PBXFileReference section */
53+
706F89492383277500ECF5D1 /* SPAlertIconErrorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPAlertIconErrorView.swift; sourceTree = "<group>"; };
5254
F41204D02382ABE9009C2AC7 /* SPAlert.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SPAlert.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5355
F41204DF2382AC9B009C2AC7 /* SPAlertView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SPAlertView.swift; sourceTree = "<group>"; };
5456
F41204E02382AC9B009C2AC7 /* SPAlertPreset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SPAlertPreset.swift; sourceTree = "<group>"; };
@@ -151,6 +153,7 @@
151153
children = (
152154
F41204E22382AC9B009C2AC7 /* SPAlertIconHeartView.swift */,
153155
F41204E32382AC9B009C2AC7 /* SPAlertIconDoneView.swift */,
156+
706F89492383277500ECF5D1 /* SPAlertIconErrorView.swift */,
154157
);
155158
path = Icons;
156159
sourceTree = "<group>";
@@ -351,6 +354,7 @@
351354
F41204EA2382AC9B009C2AC7 /* SPAlertPreset.swift in Sources */,
352355
F41204EC2382AC9B009C2AC7 /* SPAlertIconDoneView.swift in Sources */,
353356
F41204E92382AC9B009C2AC7 /* SPAlertView.swift in Sources */,
357+
706F894A2383277500ECF5D1 /* SPAlertIconErrorView.swift in Sources */,
354358
F41205292382AF5C009C2AC7 /* SPAlertIconAnimatable.swift in Sources */,
355359
F41204EB2382AC9B009C2AC7 /* SPAlertIconHeartView.swift in Sources */,
356360
F412052B2382AF64009C2AC7 /* SPAlertLayout.swift in Sources */,

Source/SPAlert/Models/SPAlertHaptic.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ import UIKit
2424
public enum SPAlertHaptic {
2525

2626
case success
27+
case error
2728
case none
2829

2930
func impact() {
31+
let generator = UINotificationFeedbackGenerator()
32+
3033
switch self {
3134
case .success:
32-
let generator = UINotificationFeedbackGenerator()
3335
generator.notificationOccurred(UINotificationFeedbackGenerator.FeedbackType.success)
36+
case .error:
37+
generator.notificationOccurred(UINotificationFeedbackGenerator.FeedbackType.error)
3438
case .none:
3539
break
3640
}

Source/SPAlert/Models/SPAlertPreset.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ public enum SPAlertPreset {
2525

2626
case done
2727
case heart
28+
case error
2829

2930
var iconView: UIView {
3031
switch self {
3132
case .done:
3233
return SPAlertIconDoneView()
3334
case .heart:
3435
return SPAlertIconHeartView()
36+
case .error:
37+
return SPAlertIconErrorView()
3538
}
3639
}
3740

@@ -53,6 +56,14 @@ public enum SPAlertPreset {
5356
layout.iconHeight = 77
5457
layout.bottomIconSpace = 35
5558
return layout
59+
case .error:
60+
var layout = SPAlertLayout()
61+
layout.topSpace = 63
62+
layout.bottomSpace = 29
63+
layout.iconWidth = 112
64+
layout.iconHeight = 112
65+
layout.bottomIconSpace = 35
66+
return layout
5667
}
5768
}
5869

@@ -62,6 +73,8 @@ public enum SPAlertPreset {
6273
return .success
6374
case .heart:
6475
return .success
76+
case .error:
77+
return .error
6578
}
6679
}
6780
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2019 Ivan Vorobei ([email protected])
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
import UIKit
23+
24+
public class SPAlertIconErrorView: UIView, SPAlertIconAnimatable {
25+
26+
public func animate() {
27+
animateTopToBottomLine()
28+
animateBottomToTopLine()
29+
}
30+
31+
private func animateTopToBottomLine() {
32+
let length = frame.width
33+
34+
let topToBottomLine = UIBezierPath()
35+
topToBottomLine.move(to: CGPoint(x: length * 0.1, y: length * 0.1))
36+
topToBottomLine.addLine(to: CGPoint(x: length * 0.9, y: length * 0.9))
37+
38+
let animatableLayer = CAShapeLayer()
39+
animatableLayer.path = topToBottomLine.cgPath
40+
animatableLayer.fillColor = UIColor.clear.cgColor
41+
animatableLayer.strokeColor = tintColor?.cgColor
42+
animatableLayer.lineWidth = 9
43+
animatableLayer.lineCap = .round
44+
animatableLayer.lineJoin = .round
45+
animatableLayer.strokeEnd = 0
46+
self.layer.addSublayer(animatableLayer)
47+
48+
let animation = CABasicAnimation(keyPath: "strokeEnd")
49+
animation.duration = 0.3
50+
animation.fromValue = 0
51+
animation.toValue = 1
52+
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
53+
54+
animatableLayer.strokeEnd = 1
55+
animatableLayer.add(animation, forKey: "animation")
56+
}
57+
58+
private func animateBottomToTopLine() {
59+
let length = frame.width
60+
61+
let bottomToTopLine = UIBezierPath()
62+
bottomToTopLine.move(to: CGPoint(x: length * 0.1, y: length * 0.9))
63+
bottomToTopLine.addLine(to: CGPoint(x: length * 0.9, y: length * 0.1))
64+
65+
let animatableLayer = CAShapeLayer()
66+
animatableLayer.path = bottomToTopLine.cgPath
67+
animatableLayer.fillColor = UIColor.clear.cgColor
68+
animatableLayer.strokeColor = tintColor?.cgColor
69+
animatableLayer.lineWidth = 9
70+
animatableLayer.lineCap = .round
71+
animatableLayer.lineJoin = .round
72+
animatableLayer.strokeEnd = 0
73+
self.layer.addSublayer(animatableLayer)
74+
75+
let animation = CABasicAnimation(keyPath: "strokeEnd")
76+
animation.duration = 0.3
77+
animation.fromValue = 0
78+
animation.toValue = 1
79+
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
80+
81+
animatableLayer.strokeEnd = 1
82+
animatableLayer.add(animation, forKey: "animation")
83+
}
84+
}

Source/SPAlert/Views/SPAlertView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ open class SPAlertView: UIView {
142142
haptic.impact()
143143
keyWindow.addSubview(self)
144144
layoutIfNeeded()
145+
layoutSubviews()
145146
alpha = 0
146147
transform = transform.scaledBy(x: 0.8, y: 0.8)
147148

@@ -226,5 +227,5 @@ open class SPAlertView: UIView {
226227
}
227228
}
228229

229-
private var keyWindow: UIWindow { return UIApplication.shared.keyWindow ?? UIWindow() }
230+
public var keyWindow: UIView = (UIApplication.shared.keyWindow ?? UIWindow())
230231
}

0 commit comments

Comments
 (0)