Skip to content

Commit 5e36d95

Browse files
committed
Added distance option to adjust distance from Turbah within settings. Added TickedSlider and is working perfectly.
1 parent 638cbdc commit 5e36d95

15 files changed

+157
-35
lines changed

Turbah.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
FAA121F324C52F2F009F12C2 /* ContactCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAA121F224C52F2F009F12C2 /* ContactCell.swift */; };
2424
FAA121FA24C69ADA009F12C2 /* ImageEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAA121F924C69ADA009F12C2 /* ImageEntity.swift */; };
2525
FAA121FC24C6A935009F12C2 /* DistanceCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAA121FB24C6A935009F12C2 /* DistanceCell.swift */; };
26+
FAA121FE24C7A4E6009F12C2 /* CustomSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAA121FD24C7A4E6009F12C2 /* CustomSlider.swift */; };
2627
FACE7EB524C15494002FE5FC /* ErrorOverlay.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACE7EB424C15494002FE5FC /* ErrorOverlay.swift */; };
2728
FACE7EB724C173FE002FE5FC /* LocationsListMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACE7EB624C173FE002FE5FC /* LocationsListMenu.swift */; };
2829
FACE7EB924C176EB002FE5FC /* Location.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACE7EB824C176EB002FE5FC /* Location.swift */; };
@@ -49,6 +50,7 @@
4950
FAA121F224C52F2F009F12C2 /* ContactCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactCell.swift; sourceTree = "<group>"; };
5051
FAA121F924C69ADA009F12C2 /* ImageEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageEntity.swift; sourceTree = "<group>"; };
5152
FAA121FB24C6A935009F12C2 /* DistanceCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DistanceCell.swift; sourceTree = "<group>"; };
53+
FAA121FD24C7A4E6009F12C2 /* CustomSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomSlider.swift; sourceTree = "<group>"; };
5254
FACE7EB424C15494002FE5FC /* ErrorOverlay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorOverlay.swift; sourceTree = "<group>"; };
5355
FACE7EB624C173FE002FE5FC /* LocationsListMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListMenu.swift; sourceTree = "<group>"; };
5456
FACE7EB824C176EB002FE5FC /* Location.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Location.swift; sourceTree = "<group>"; };
@@ -132,6 +134,7 @@
132134
FAA121F024C50CAC009F12C2 /* SegmentedCell.swift */,
133135
FAA121F224C52F2F009F12C2 /* ContactCell.swift */,
134136
FAA121FB24C6A935009F12C2 /* DistanceCell.swift */,
137+
FAA121FD24C7A4E6009F12C2 /* CustomSlider.swift */,
135138
);
136139
path = Settings;
137140
sourceTree = "<group>";
@@ -221,6 +224,7 @@
221224
FAA121EE24C50C4A009F12C2 /* UserDefaults.swift in Sources */,
222225
FA57BA6C24BFB314009FC13E /* Turbah.rcproject in Sources */,
223226
FAA121F124C50CAC009F12C2 /* SegmentedCell.swift in Sources */,
227+
FAA121FE24C7A4E6009F12C2 /* CustomSlider.swift in Sources */,
224228
FACE7EC324C411FF002FE5FC /* LocationListCell.swift in Sources */,
225229
FA51865724C13A56009064F6 /* CustomTurbah.swift in Sources */,
226230
FACE7EC124C4006D002FE5FC /* CompassView.swift in Sources */,

Turbah/AppDelegate.swift

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1616
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1717
// Override point for customization after application launch.
1818
setupScreen()
19+
setupDefaults()
1920
return true
2021
}
2122

@@ -47,5 +48,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4748

4849
window?.rootViewController = ViewController()
4950
}
51+
52+
func setupDefaults() {
53+
save.register(defaults: [
54+
"TurbahDistance" : 3
55+
])
56+
}
5057

5158
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"filename" : "thumb@2x.png",
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"filename" : "thumb@3x.png",
14+
"idiom" : "universal",
15+
"scale" : "3x"
16+
}
17+
],
18+
"info" : {
19+
"author" : "xcode",
20+
"version" : 1
21+
}
22+
}
401 Bytes
Loading
538 Bytes
Loading

Turbah/Classes/LocationsListMenu.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class LocationsListMenu: UITableView, UITableViewDelegate, UITableViewDataSource
1515
let locations = Locations.allLocations
1616
let cellHeight: CGFloat = 47
1717

18-
var height: CGFloat { return cellHeight * CGFloat(locations.count) }
18+
var height: CGFloat { return cellHeight * CGFloat(min(locations.count, 10)) }
1919
let width: CGFloat = 250
2020

2121
private func setupUI() {
@@ -26,7 +26,8 @@ class LocationsListMenu: UITableView, UITableViewDelegate, UITableViewDataSource
2626

2727
delegate = self
2828
dataSource = self
29-
isScrollEnabled = false
29+
alwaysBounceVertical = false
30+
verticalScrollIndicatorInsets = UIEdgeInsets(top: 7.5, left: 0, bottom: 7.5, right: 0)
3031
backgroundColor = nil
3132
backgroundView = UIVisualEffectView(effect: blurEffect)
3233
separatorEffect = UIVibrancyEffect(blurEffect: blurEffect, style: .separator)

Turbah/Classes/Settings/ContactCell.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ContactCell: UITableViewCell {
3030
stack.spacing = 15
3131
stack.addArrangedSubview(iconImageView)
3232
stack.addArrangedSubview(infoLabel)
33-
addSubview(stack)
33+
contentView.addSubview(stack)
3434
stack.fillSuperview(padding: UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15))
3535
}
3636

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// CustomSlider.swift
3+
// Turbah
4+
//
5+
// Created by MMQ on 7/21/20.
6+
// Copyright © 2020 MMQ. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class TickedSlider: UISlider {
12+
13+
var pathHeight: CGFloat = 2
14+
var tickColor: UIColor = UIColor { ($0.userInterfaceStyle == .dark) ? UIColor(hexString: "3D3D42") : UIColor(hexString: "EEEEEE") }//.separator//#colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
15+
var tickWidth: CGFloat = 10
16+
17+
var trackRect: CGRect {
18+
var returnRect = trackRect(forBounds: bounds)
19+
returnRect.origin.x += 2
20+
returnRect.size.width -= 4
21+
return returnRect
22+
}
23+
var pathWidth: CGFloat {
24+
return bounds.size.width - 4
25+
}
26+
var tickDistance: Double {
27+
return (Double(pathWidth) / Double(ticks - 1)) - 1
28+
}
29+
var ticks: Int {
30+
return Int(maximumValue - minimumValue) + 1
31+
}
32+
33+
override func draw(_ rect: CGRect) {
34+
let context = UIGraphicsGetCurrentContext()
35+
context?.saveGState()
36+
37+
UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: pathHeight), false, 0.0)
38+
let transparentImage = UIGraphicsGetImageFromCurrentImageContext()
39+
UIGraphicsEndImageContext()
40+
setMaximumTrackImage(transparentImage, for: .normal)
41+
setMinimumTrackImage(transparentImage, for: .normal)
42+
43+
context?.setFillColor(tickColor.cgColor)
44+
45+
let path = UIBezierPath(rect: trackRect)
46+
context?.addPath(path.cgPath)
47+
context?.fillPath()
48+
49+
// Draw Ticks
50+
51+
context?.setFillColor(tickColor.cgColor)
52+
53+
for index in 1...ticks {
54+
let x = CGFloat(Double(index - 1) * tickDistance)
55+
let y = bounds.midY - CGFloat(8 / 2)
56+
let stepPath = UIBezierPath(rect: CGRect(x: x+2, y: y, width: 2, height: 8))
57+
58+
context?.addPath(stepPath.cgPath)
59+
context?.fillPath()
60+
}
61+
}
62+
}

Turbah/Classes/Settings/DistanceCell.swift

+25-4
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,39 @@ class DistanceCell: UITableViewCell {
1414

1515
private var stack = UIStackView()
1616
var titleLabel = UILabel()
17-
var distanceSlider = UISlider()
17+
var distanceSlider = TickedSlider()
1818

1919
private func setupUI() {
20+
let reverseColor = UIColor { return $0.userInterfaceStyle == .dark ? .white : .black }
21+
22+
titleLabel.text = "Turbah distance"
23+
24+
distanceSlider.minimumValue = 1
25+
distanceSlider.maximumValue = 5
26+
distanceSlider.value = save.distance
27+
distanceSlider.minimumTrackTintColor = reverseColor
28+
distanceSlider.setThumbImage(#imageLiteral(resourceName: "thumb").withTintColor(reverseColor), for: .normal)
29+
distanceSlider.addTarget(self, action: #selector(sliderValueChanged(_:_:)), for: .valueChanged)
30+
distanceSlider.translatesAutoresizingMaskIntoConstraints = false
31+
distanceSlider.heightAnchor.constraint(equalToConstant: 12).isActive = true
32+
2033
stack.axis = .vertical
21-
stack.alignment = .leading
22-
stack.distribution = .fillProportionally
34+
stack.alignment = .fill
35+
stack.distribution = .fill
2336
stack.spacing = 10
2437
stack.addArrangedSubview(titleLabel)
2538
stack.addArrangedSubview(distanceSlider)
26-
addSubview(stack)
39+
contentView.addSubview(stack)
2740
stack.fillSuperview(padding: UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15))
2841
}
42+
43+
@objc private func sliderValueChanged(_ sender: UISlider, _ event: UIEvent) {
44+
let rounded = round(sender.value)
45+
sender.value = rounded
46+
guard rounded != save.distance else { return }
47+
hapticFeedback()
48+
save.distance = rounded
49+
}
2950

3051
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
3152
super.init(style: style, reuseIdentifier: reuseIdentifier)

Turbah/Classes/Settings/SegmentedCell.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class SegmentedCell: UITableViewCell {
1717
private func setupUI() {
1818
segmentedControl.insertSegment(withTitle: "True North", at: 0, animated: false)
1919
segmentedControl.insertSegment(withTitle: "Magnetic North", at: 1, animated: false)
20-
segmentedControl.selectedSegmentIndex = (save.northType == .trueNorth) ? 0 : 1
21-
addSubview(segmentedControl)
20+
segmentedControl.selectedSegmentIndex = save.trueNorth ? 0 : 1
21+
contentView.addSubview(segmentedControl)
2222
segmentedControl.fillSuperview(padding: UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15))
2323
}
2424

Turbah/Classes/UserDefaults.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ import Foundation
1010

1111
extension UserDefaults {
1212

13-
enum northTypeEnum { case trueNorth, magneticNorth }
14-
var northType: northTypeEnum {
13+
var trueNorth: Bool {
1514
get {
16-
return (save.integer(forKey: "NorthType") == 0) ? .trueNorth : .magneticNorth
15+
return save.bool(forKey: "NorthType")
1716
}
1817
set {
19-
save.set((newValue == .trueNorth) ? 0 : 1, forKey: "NorthType")
18+
save.set(newValue, forKey: "NorthType")
19+
}
20+
}
21+
22+
var distance: Float {
23+
get {
24+
return save.float(forKey: "TurbahDistance")
25+
}
26+
set {
27+
save.set(newValue, forKey: "TurbahDistance")
2028
}
2129
}
2230

Turbah/Controllers/SettingsVC.swift

+11-13
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,17 @@ class SettingsVC: UITableViewController, MFMailComposeViewControllerDelegate {
2828

2929
enum cellNames {
3030
case northType
31+
case distance
3132
case contact
3233
case terms
3334
case share
34-
case red
3535
}
3636
}
3737

3838
let sectionArray = [
3939
section(name: .general, cells: [
40-
cell(name: .northType),
41-
cell(name: .red),
42-
cell(name: .red),
43-
cell(name: .red)
40+
cell(name: .distance),
41+
cell(name: .northType)
4442
]),
4543
section(name: .contact, cells: [
4644
cell(name: .contact),
@@ -57,13 +55,13 @@ class SettingsVC: UITableViewController, MFMailComposeViewControllerDelegate {
5755
override func viewDidLoad() {
5856
super.viewDidLoad()
5957
title = "Settings"
60-
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(donePressed))
58+
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(donePressed))
6159

62-
tableView = UITableView(frame: self.tableView.frame, style: .insetGrouped)
60+
tableView = UITableView(frame: tableView.frame, style: .insetGrouped)
6361
tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.cellID)
6462
tableView.register(SegmentedCell.self, forCellReuseIdentifier: SegmentedCell.id)
6563
tableView.register(ContactCell.self, forCellReuseIdentifier: ContactCell.id)
66-
//tableView.sectionHeaderHeight = 70
64+
tableView.register(DistanceCell.self, forCellReuseIdentifier: DistanceCell.id)
6765
}
6866

6967
@objc func donePressed() {
@@ -77,6 +75,10 @@ class SettingsVC: UITableViewController, MFMailComposeViewControllerDelegate {
7775
cell.selectionStyle = .none
7876
cell.segmentedControl.addTarget(self, action: #selector(northTypeChanged(_:)), for: .valueChanged)
7977
return cell
78+
case .distance:
79+
let cell = tableView.dequeueReusableCell(withIdentifier: DistanceCell.id, for: indexPath) as! DistanceCell
80+
cell.selectionStyle = .none
81+
return cell
8082
case .contact:
8183
let cell = tableView.dequeueReusableCell(withIdentifier: ContactCell.id, for: indexPath) as! ContactCell
8284
cell.iconImageView.image = contactInfo[indexPath.row].0
@@ -90,10 +92,6 @@ class SettingsVC: UITableViewController, MFMailComposeViewControllerDelegate {
9092
let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.cellID, for: indexPath)
9193
cell.textLabel?.text = "Share"
9294
return cell
93-
case .red:
94-
let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.cellID, for: indexPath)
95-
cell.textLabel?.text = "y\(indexPath.row)lo"
96-
return cell
9795
}
9896
}
9997

@@ -168,7 +166,7 @@ class SettingsVC: UITableViewController, MFMailComposeViewControllerDelegate {
168166

169167

170168
@objc private func northTypeChanged(_ sender: UISegmentedControl) {
171-
save.northType = (sender.selectedSegmentIndex == 0) ? .trueNorth : .magneticNorth
169+
save.trueNorth = sender.selectedSegmentIndex == 0
172170
}
173171

174172
}

Turbah/Controllers/ViewController.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ class ViewController: UIViewController, CLLocationManagerDelegate, ARCoachingOve
8787

8888
let cameraAngles = arView.session.currentFrame!.camera.transform.columns.3
8989

90-
let decreaseValue: Double = 3
90+
let decreaseValue: Float = 6 - save.distance
9191

92-
anchor.position.z = Float(-1 * cos(bearingOfKabah) / decreaseValue) + cameraAngles.z
93-
anchor.position.x = Float(sin(bearingOfKabah) / decreaseValue) + cameraAngles.x
92+
anchor.position.z = Float(-1 * cos(Float(bearingOfKabah)) / decreaseValue) + cameraAngles.z
93+
anchor.position.x = Float(sin(Float(bearingOfKabah)) / decreaseValue) + cameraAngles.x
9494
anchor.position.y += cameraAngles.y
9595
print("after", anchor.position)
9696

@@ -260,7 +260,7 @@ class ViewController: UIViewController, CLLocationManagerDelegate, ARCoachingOve
260260
super.viewWillAppear(animated)
261261
let config = ARWorldTrackingConfiguration()
262262
config.planeDetection = .horizontal
263-
config.worldAlignment = (save.northType == .trueNorth) ? .gravityAndHeading : .gravity
263+
config.worldAlignment = save.trueNorth ? .gravityAndHeading : .gravity
264264
arView.session.run(config)
265265
}
266266

@@ -298,14 +298,14 @@ class ViewController: UIViewController, CLLocationManagerDelegate, ARCoachingOve
298298
var didAddCoaching = false
299299

300300
func locationManager(_ manager: CLLocationManager, didUpdateHeading heading: CLHeading) {
301-
let chosenHeading = (save.northType == .trueNorth) ? heading.trueHeading : heading.magneticHeading
301+
let chosenHeading = save.trueNorth ? heading.trueHeading : heading.magneticHeading
302302
let north = chosenHeading.degreesToRadians
303303
let directionOfKabah = north - bearingOfKabah
304304

305305
//print("qibla degreees:", directionOfKabah.radiansToDegrees)
306-
print("phone", arView.session.currentFrame?.camera.transform.columns.3 ?? "")
307-
print("anchor", anchor.transform.matrix.columns.3)
308-
print("")
306+
//print("phone", arView.session.currentFrame?.camera.transform.columns.3 ?? "")
307+
//print("anchor", anchor.transform.matrix.columns.3)
308+
//print("")
309309

310310
compassView.kabaImageView.transform = CGAffineTransform(rotationAngle: CGFloat(-directionOfKabah));
311311
compassView.isPointingAtQibla = (directionOfKabah < 0.1 && directionOfKabah > -0.1)

Turbah/Super.swift

-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ extension UIView {
9191
}
9292

9393

94-
9594
func hapticFeedback(style: UIImpactFeedbackGenerator.FeedbackStyle = .light) {
9695
UIImpactFeedbackGenerator(style: style).impactOccurred()
9796
}

0 commit comments

Comments
 (0)