Skip to content

Commit 706e86e

Browse files
committed
0.1.4
1 parent ac88659 commit 706e86e

4 files changed

Lines changed: 73 additions & 38 deletions

File tree

Example/TutorialKitSwift/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ class ViewController: UIViewController {
3939
// Dispose of any resources that can be recreated.
4040
}
4141
override func viewDidAppear(_ animated: Bool) {
42-
self.showTutorial(ofView: self.button, text: "Hey bae, How you doin?", direction: .down, tutKey: "test",mode: .always, bubbleColor:UIColor.systemBlue)
42+
self.showTutorial(ofView: button, text: "Hey bae, How you doin?", direction: .auto, tutKey: "test1",mode: .always, bubbleColor:UIColor.systemBlue)
4343
}
4444

4545
}
46-
extension ViewController:TutorialViewDelegate{
47-
func didFinishTut(tutKey: String) {
46+
extension ViewController:TutorialKitSwiftDelegate{
47+
func didDismissTut(tutKey: String) {
4848
print(tutKey)
4949
}
5050
}

Source/TutorialView.swift

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,37 @@
77

88
import UIKit
99
import SnapKit
10+
public class TutorialKitSwift{
11+
func showTutorial(ofView view: UIView,text:String, direction: Direction, tutKey:String, mode:TutorialShowMode = .once,bubbleColor:UIColor = UIColor.systemBlue){
12+
13+
let frame = view.getFrameOnScreen()
14+
guard let window = UIApplication.shared.keyWindow else{
15+
return
16+
}
1017

18+
let circleRadius = sqrt(pow(frame.width, 2) + pow(frame.height, 2))/2.0 * 1.2
19+
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)
20+
let image = window.takeSnapshot(newFrame)
21+
22+
let tutView = TutorialView(ofView: UIImageView(image: image), frame: newFrame, text: text, direction: direction, tutKey:tutKey,bubbleColor:bubbleColor, delegate: self as? TutorialKitSwiftDelegate)
23+
// self.view.addSubview(tutView)
24+
// tutView.snp.remakeConstraints({remake in
25+
// remake.edges.equalToSuperview()
26+
// })
27+
if view.isHidden || (UserDefaults.standard.bool(forKey: tutKey) && mode == .once) {
28+
tutView.nextTut()
29+
}else{
30+
window.addSubview(tutView)
31+
tutView.snp.remakeConstraints({remake in
32+
remake.edges.equalToSuperview()
33+
})
34+
window.setNeedsLayout()
35+
window.layoutIfNeeded()
36+
tutView.popInstruction()
37+
38+
}
39+
}
40+
}
1141

1242
class TutorialView: UIView {
1343

@@ -18,20 +48,20 @@ class TutorialView: UIView {
1848
super.init(coder: coder)
1949
}
2050
let popTip = PopTip()
21-
var delegate:TutorialViewDelegate?
51+
var delegate:TutorialKitSwiftDelegate?
2252
var direction:Direction!
2353
var tutKey:String!
2454
var interactView = UIImageView()
2555
var text:String!
2656

2757
@objc func nextTut(){
2858
DispatchQueue.main.async {
29-
self.delegate?.didFinishTut(tutKey: self.tutKey)
59+
self.delegate?.didDismissTut(tutKey: self.tutKey)
3060
UserDefaults.standard.set(true, forKey: self.tutKey)
3161
self.removeFromSuperview()
3262
}
3363
}
34-
init(ofView view:UIView,frame:CGRect,text:String, direction:Direction, tutKey:String,bubbleColor:UIColor = UIColor.systemBlue, delegate:TutorialViewDelegate? = nil) {
64+
init(ofView view:UIView,frame:CGRect,text:String, direction:Direction, tutKey:String,bubbleColor:UIColor = UIColor.systemBlue, delegate:TutorialKitSwiftDelegate? = nil) {
3565
super.init(frame: CGRect.zero)
3666
self.delegate = delegate
3767
self.direction = direction
@@ -74,6 +104,9 @@ class TutorialView: UIView {
74104
case .down:
75105
popTip.show(text: self.text, direction: .down, maxWidth: 200, in: self, from: self.interactView.frame)
76106
break
107+
case .auto:
108+
popTip.show(text: self.text, direction: .auto, maxWidth: 200, in: self, from: self.interactView.frame)
109+
break
77110
default:
78111
break
79112
}

Source/extensions + enums + protocols.swift

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,45 @@
88
import Foundation
99
import UIKit
1010

11-
public protocol TutorialViewDelegate{
12-
func didFinishTut(tutKey:String)
11+
public protocol TutorialKitSwiftDelegate{
12+
func didDismissTut(tutKey:String)
1313
}
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+
}
1421

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+
}
1545
public enum TutorialShowMode{
1646
case always, once
1747
}
1848
public enum Direction {
19-
case up,down
49+
case up,down, auto
2050
}
2151

2252
extension UIView{
@@ -58,32 +88,4 @@ extension UIView{
5888
}
5989

6090
}
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-
}
8991

TutorialKitSwift.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'TutorialKitSwift'
11-
s.version = '0.1.3'
11+
s.version = '0.1.4'
1212
s.summary = 'TutorialKitSwift.'
1313

1414
# This description is used to generate tags and improve search results.

0 commit comments

Comments
 (0)