|
8 | 8 | import Foundation |
9 | 9 | import UIKit |
10 | 10 |
|
11 | | -public protocol TutorialViewDelegate{ |
12 | | - func didFinishTut(tutKey:String) |
| 11 | +public protocol TutorialKitSwiftDelegate{ |
| 12 | + func didDismissTut(tutKey:String) |
13 | 13 | } |
| 14 | +extension TutorialKitSwiftDelegate{ |
| 15 | + public func showTutorial(ofView view: UIView,text:String, direction: Direction, tutKey:String, mode:TutorialShowMode = .once,bubbleColor:UIColor = UIColor.systemBlue){ |
| 16 | + |
| 17 | + let frame = view.getFrameOnScreen() |
| 18 | + guard let window = UIApplication.shared.keyWindow else{ |
| 19 | + return |
| 20 | + } |
14 | 21 |
|
| 22 | + let circleRadius = sqrt(pow(frame.width, 2) + pow(frame.height, 2))/2.0 * 1.2 |
| 23 | + let newFrame = CGRect(x: frame.origin.x - (2.0 * circleRadius - frame.width)/2.0, y: frame.origin.y - (2.0 * circleRadius - frame.height)/2.0, width: 2.0 * circleRadius, height: 2.0 * circleRadius) |
| 24 | + let image = window.takeSnapshot(newFrame) |
| 25 | + |
| 26 | + let tutView = TutorialView(ofView: UIImageView(image: image), frame: newFrame, text: text, direction: direction, tutKey:tutKey,bubbleColor:bubbleColor, delegate: self as? TutorialKitSwiftDelegate) |
| 27 | + // self.view.addSubview(tutView) |
| 28 | + // tutView.snp.remakeConstraints({remake in |
| 29 | + // remake.edges.equalToSuperview() |
| 30 | + // }) |
| 31 | + if view.isHidden || (UserDefaults.standard.bool(forKey: tutKey) && mode == .once) { |
| 32 | + tutView.nextTut() |
| 33 | + }else{ |
| 34 | + window.addSubview(tutView) |
| 35 | + tutView.snp.remakeConstraints({remake in |
| 36 | + remake.edges.equalToSuperview() |
| 37 | + }) |
| 38 | + window.setNeedsLayout() |
| 39 | + window.layoutIfNeeded() |
| 40 | + tutView.popInstruction() |
| 41 | + |
| 42 | + } |
| 43 | + } |
| 44 | +} |
15 | 45 | public enum TutorialShowMode{ |
16 | 46 | case always, once |
17 | 47 | } |
18 | 48 | public enum Direction { |
19 | | - case up,down |
| 49 | + case up,down, auto |
20 | 50 | } |
21 | 51 |
|
22 | 52 | extension UIView{ |
@@ -58,32 +88,4 @@ extension UIView{ |
58 | 88 | } |
59 | 89 |
|
60 | 90 | } |
61 | | -public extension UIViewController{ |
62 | | - func showTutorial(ofView view: UIView,text:String, direction: Direction, tutKey:String, mode:TutorialShowMode = .once,bubbleColor:UIColor = UIColor.systemBlue){ |
63 | | - |
64 | | - let frame = view.getFrameOnScreen() |
65 | | - |
66 | | - let circleRadius = sqrt(pow(frame.width, 2) + pow(frame.height, 2))/2.0 * 1.2 |
67 | | - let newFrame = CGRect(x: frame.origin.x - (2.0 * circleRadius - frame.width)/2.0, y: frame.origin.y - (2.0 * circleRadius - frame.height)/2.0, width: 2.0 * circleRadius, height: 2.0 * circleRadius) |
68 | | - let image = self.view.takeSnapshot(newFrame) |
69 | | - |
70 | | - let tutView = TutorialView(ofView: UIImageView(image: image), frame: newFrame, text: text, direction: direction, tutKey:tutKey,bubbleColor:bubbleColor, delegate: self as? TutorialViewDelegate) |
71 | | - // self.view.addSubview(tutView) |
72 | | - // tutView.snp.remakeConstraints({remake in |
73 | | - // remake.edges.equalToSuperview() |
74 | | - // }) |
75 | | - if view.isHidden || (UserDefaults.standard.bool(forKey: tutKey) && mode == .once) { |
76 | | - tutView.nextTut() |
77 | | - }else{ |
78 | | - self.view.addSubview(tutView) |
79 | | - tutView.snp.remakeConstraints({remake in |
80 | | - remake.edges.equalToSuperview() |
81 | | - }) |
82 | | - self.view.setNeedsLayout() |
83 | | - self.view.layoutIfNeeded() |
84 | | - tutView.popInstruction() |
85 | | - |
86 | | - } |
87 | | - } |
88 | | -} |
89 | 91 |
|
0 commit comments