Skip to content

Commit 721cf95

Browse files
committed
MotionToast_Customisation
1 parent f89ddbd commit 721cf95

File tree

4 files changed

+107
-30
lines changed

4 files changed

+107
-30
lines changed

MotionToastView.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'MotionToastView'
11-
s.version = '0.1.7'
12-
s.summary = 'A Beautiful Toast Library for iOS Swift'
11+
s.version = '0.1.8'
12+
s.summary = 'A Beautiful Toast Library for iOS using Swift'
1313

1414
# This description is used to generate tags and improve search results.
1515
# * Think: What does it do? Why did you write it? What is the focus?
@@ -18,7 +18,7 @@ Pod::Spec.new do |s|
1818
# * Finally, don't worry about the indent, CocoaPods strips it!
1919

2020
s.description = <<-DESC
21-
'A Beautiful Motion Toast Library for iOS Swift'
21+
'A Beautiful Motion Toast Library for iOS using Swift'
2222
DESC
2323

2424
s.homepage = 'https://github.com/sameersyd/MotionToastView'

Source/Extension.swift

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public enum ToastStyle {
3333

3434
extension UIViewController {
3535

36-
public func MotionToast(message: String, toastType: ToastType, duration: ToastDuration? = .short, toastStyle: ToastStyle? = .style_vibrant, toastGravity: ToastGravity? = .bottom, toastCornerRadius: Int? = 0) {
36+
public func MotionToast(message: String, toastType: ToastType, duration: ToastDuration? = .short, toastStyle: ToastStyle? = .style_vibrant, toastGravity: ToastGravity? = .bottom, toastCornerRadius: Int? = 0, pulseEffect: Bool? = true) {
3737

3838
guard let window = UIApplication.shared.connectedScenes.filter({$0.activationState == .foregroundActive}).map({$0 as? UIWindowScene})
3939
.compactMap({$0}).first?.windows.filter({$0.isKeyWindow}).first else { return }
@@ -47,8 +47,8 @@ extension UIViewController {
4747

4848
var toastView: UIView?
4949
switch toastStyle {
50-
case .style_vibrant: toastView = toastStyle_vibrant(message: message, toastType: toastType, toastGravity: toastGravity!, toastCornerRadius: toastCornerRadius!, view: view);break
51-
case .style_pale: toastView = toastStyle_pale(message: message, toastType: toastType, toastGravity: toastGravity!, view: view);break
50+
case .style_vibrant: toastView = toastStyle_vibrant(message: message, toastType: toastType, toastGravity: toastGravity!, toastCornerRadius: toastCornerRadius!, view: view, pulseEffect: pulseEffect!);break
51+
case .style_pale: toastView = toastStyle_pale(message: message, toastType: toastType, toastGravity: toastGravity!, view: view, pulseEffect: pulseEffect!);break
5252
case .none: break
5353
}
5454

@@ -60,9 +60,84 @@ extension UIViewController {
6060
toastView!.removeFromSuperview()
6161
}
6262
}
63+
64+
public func MotionToast_Customisation(header: String, message: String, headerColor: UIColor, messageColor: UIColor,
65+
primary_color: UIColor, secondary_color: UIColor, icon_image: UIImage,
66+
duration: ToastDuration? = .short, toastStyle: ToastStyle? = .style_vibrant,
67+
toastGravity: ToastGravity? = .bottom, toastCornerRadius: Int? = 0, pulseEffect: Bool? = true) {
68+
69+
guard let window = UIApplication.shared.connectedScenes.filter({$0.activationState == .foregroundActive}).map({$0 as? UIWindowScene})
70+
.compactMap({$0}).first?.windows.filter({$0.isKeyWindow}).first else { return }
71+
72+
var toastDuration = 2.0
73+
switch duration {
74+
case .short: toastDuration = 2.0;break
75+
case .long: toastDuration = 4.0;break
76+
case .none: break
77+
}
78+
79+
var toastUIView: UIView?
80+
switch toastStyle {
81+
case .style_vibrant:
82+
83+
var gravity = CGRect(x: 0.0, y: view.frame.height - 130.0, width: view.frame.width, height: 83.0)
84+
switch toastGravity! {
85+
case .top: gravity = CGRect(x: 0.0, y: 80.0, width: view.frame.width, height: 83.0);break
86+
case .centre: gravity = CGRect(x: 0.0, y: ((view.frame.height / 2) - 41) , width: view.frame.width, height: 83.0);break
87+
case .bottom: gravity = CGRect(x: 0.0, y: view.frame.height - 130.0, width: view.frame.width, height: 83.0);break
88+
}
89+
90+
let toastView = MTVibrant(frame: gravity)
91+
if pulseEffect! { toastView.addPulseEffect() }
92+
toastView.toastView.layer.cornerRadius = CGFloat(toastCornerRadius!)
93+
94+
toastView.headLabel.text = header
95+
toastView.headLabel.textColor = headerColor
96+
toastView.msgLabel.text = message
97+
toastView.msgLabel.textColor = messageColor
98+
toastView.circleImg.image = icon_image
99+
toastView.toastView.backgroundColor = primary_color
100+
toastView.circleView.backgroundColor = secondary_color
101+
toastUIView = toastView
102+
break
103+
104+
case .style_pale:
105+
106+
var gravity = CGRect(x: 0.0, y: view.frame.height - 130.0, width: view.frame.width, height: 83.0)
107+
switch toastGravity! {
108+
case .top: gravity = CGRect(x: 0.0, y: 80.0, width: view.frame.width, height: 83.0);break
109+
case .centre: gravity = CGRect(x: 0.0, y: ((view.frame.height / 2) - 41) , width: view.frame.width, height: 83.0);break
110+
case .bottom: gravity = CGRect(x: 0.0, y: view.frame.height - 130.0, width: view.frame.width, height: 83.0);break
111+
}
112+
113+
let toastView = MTPale(frame: gravity)
114+
if pulseEffect! { toastView.addPulseEffect() }
115+
toastView.toastView.layer.cornerRadius = CGFloat(toastCornerRadius!)
116+
117+
toastView.headLabel.text = header
118+
toastView.headLabel.textColor = headerColor
119+
toastView.msgLabel.text = message
120+
toastView.msgLabel.textColor = messageColor
121+
toastView.circleImg.image = icon_image
122+
toastView.toastView.backgroundColor = primary_color
123+
toastView.circleView.backgroundColor = secondary_color
124+
toastView.sideBarView.backgroundColor = secondary_color
125+
toastUIView = toastView
126+
break
127+
case .none: break
128+
}
129+
130+
window.addSubview(toastUIView!)
131+
132+
UIView.animate(withDuration: 1.0, delay: toastDuration, animations: {
133+
toastUIView!.alpha = 0
134+
}) { (_) in
135+
toastUIView!.removeFromSuperview()
136+
}
137+
}
63138
}
64139

65-
func toastStyle_vibrant(message: String, toastType: ToastType, toastGravity: ToastGravity, toastCornerRadius: Int, view: UIView) -> MTVibrant {
140+
func toastStyle_vibrant(message: String, toastType: ToastType, toastGravity: ToastGravity, toastCornerRadius: Int, view: UIView, pulseEffect: Bool) -> MTVibrant {
66141

67142
var gravity = CGRect(x: 0.0, y: view.frame.height - 130.0, width: view.frame.width, height: 83.0)
68143
switch toastGravity {
@@ -73,12 +148,13 @@ func toastStyle_vibrant(message: String, toastType: ToastType, toastGravity: Toa
73148

74149
let toastView = MTVibrant(frame: gravity)
75150
toastView.setupViews(toastType: toastType)
151+
if pulseEffect { toastView.addPulseEffect() }
76152
toastView.msgLabel.text = message
77153
toastView.toastView.layer.cornerRadius = CGFloat(toastCornerRadius)
78154
return toastView
79155
}
80156

81-
func toastStyle_pale(message: String, toastType: ToastType, toastGravity: ToastGravity, view: UIView) -> MTPale {
157+
func toastStyle_pale(message: String, toastType: ToastType, toastGravity: ToastGravity, view: UIView, pulseEffect: Bool) -> MTPale {
82158

83159
var gravity = CGRect(x: 0.0, y: view.frame.height - 130.0, width: view.frame.width, height: 83.0)
84160
switch toastGravity {
@@ -89,6 +165,7 @@ func toastStyle_pale(message: String, toastType: ToastType, toastGravity: ToastG
89165

90166
let toastView = MTPale(frame: gravity)
91167
toastView.setupViews(toastType: toastType)
168+
if pulseEffect { toastView.addPulseEffect() }
92169
toastView.msgLabel.text = message
93170
return toastView
94171
}

Source/MotionToastStyles/MTPale.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ class MTPale: UIView {
2323
sideBarView.layer.cornerRadius = 3
2424
toastView.layer.cornerRadius = 12
2525
circleView.layer.cornerRadius = circleView.bounds.size.width/2
26-
27-
let pulseAnimation = CABasicAnimation(keyPath: "transform.scale")
28-
pulseAnimation.duration = 1
29-
pulseAnimation.fromValue = 0.7
30-
pulseAnimation.toValue = 1
31-
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
32-
pulseAnimation.autoreverses = true
33-
pulseAnimation.repeatCount = .greatestFiniteMagnitude
34-
circleImg.layer.add(pulseAnimation, forKey: "animateOpacity")
3526
}
3627

3728
required init?(coder: NSCoder) {
@@ -46,6 +37,17 @@ class MTPale: UIView {
4637
addSubview(viewFromXib)
4738
}
4839

40+
func addPulseEffect() {
41+
let pulseAnimation = CABasicAnimation(keyPath: "transform.scale")
42+
pulseAnimation.duration = 1
43+
pulseAnimation.fromValue = 0.7
44+
pulseAnimation.toValue = 1
45+
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
46+
pulseAnimation.autoreverses = true
47+
pulseAnimation.repeatCount = .greatestFiniteMagnitude
48+
circleImg.layer.add(pulseAnimation, forKey: "animateOpacity")
49+
}
50+
4951
func setupViews(toastType: ToastType) {
5052
switch toastType {
5153
case .success:
@@ -85,7 +87,6 @@ class MTPale: UIView {
8587
let bundle = Bundle(url: url)
8688
return UIImage(named: name, in: bundle, compatibleWith: nil)
8789
}
88-
print("MotionToastView Image")
8990
return nil
9091
}
9192

@@ -95,7 +96,6 @@ class MTPale: UIView {
9596
let bundle = Bundle(url: url)
9697
return UIColor(named: name, in: bundle, compatibleWith: nil)
9798
}
98-
print("MotionToastView Color")
9999
return nil
100100
}
101101
}

Source/MotionToastStyles/MTVibrant.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ class MTVibrant: UIView {
2020
super.init(frame: frame)
2121
commonInit()
2222
circleView.layer.cornerRadius = circleView.bounds.size.width/2
23-
24-
let pulseAnimation = CABasicAnimation(keyPath: "transform.scale")
25-
pulseAnimation.duration = 1
26-
pulseAnimation.fromValue = 0.7
27-
pulseAnimation.toValue = 1
28-
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
29-
pulseAnimation.autoreverses = true
30-
pulseAnimation.repeatCount = .greatestFiniteMagnitude
31-
circleImg.layer.add(pulseAnimation, forKey: "animateOpacity")
3223
}
3324

3425
required init?(coder: NSCoder) {
@@ -43,6 +34,17 @@ class MTVibrant: UIView {
4334
addSubview(viewFromXib)
4435
}
4536

37+
func addPulseEffect() {
38+
let pulseAnimation = CABasicAnimation(keyPath: "transform.scale")
39+
pulseAnimation.duration = 1
40+
pulseAnimation.fromValue = 0.7
41+
pulseAnimation.toValue = 1
42+
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
43+
pulseAnimation.autoreverses = true
44+
pulseAnimation.repeatCount = .greatestFiniteMagnitude
45+
circleImg.layer.add(pulseAnimation, forKey: "animateOpacity")
46+
}
47+
4648
func setupViews(toastType: ToastType) {
4749
switch toastType {
4850
case .success:
@@ -78,7 +80,6 @@ class MTVibrant: UIView {
7880
let bundle = Bundle(url: url)
7981
return UIImage(named: name, in: bundle, compatibleWith: nil)
8082
}
81-
print("MotionToastView Image")
8283
return nil
8384
}
8485

@@ -88,7 +89,6 @@ class MTVibrant: UIView {
8889
let bundle = Bundle(url: url)
8990
return UIColor(named: name, in: bundle, compatibleWith: nil)
9091
}
91-
print("MotionToastView Color")
9292
return nil
9393
}
9494
}

0 commit comments

Comments
 (0)