Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions SnowGlobe/SnowGlobe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
9FDC70E11A2FBFDC0033F371 /* Numbers+Extentios.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FDC70DB1A2FBFDC0033F371 /* Numbers+Extentios.swift */; };
9FDC70E21A2FBFDC0033F371 /* SleighBells.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = 9FDC70DC1A2FBFDC0033F371 /* SleighBells.mp3 */; };
9FDC70E31A2FBFDC0033F371 /* SnowGlobeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FDC70DD1A2FBFDC0033F371 /* SnowGlobeView.swift */; };
A774FBD61FB32DD000D08796 /* SnowGlobeSensitivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = A774FBD51FB32DD000D08796 /* SnowGlobeSensitivity.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -41,6 +42,7 @@
9FDC70DB1A2FBFDC0033F371 /* Numbers+Extentios.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Numbers+Extentios.swift"; sourceTree = "<group>"; };
9FDC70DC1A2FBFDC0033F371 /* SleighBells.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = SleighBells.mp3; sourceTree = "<group>"; };
9FDC70DD1A2FBFDC0033F371 /* SnowGlobeView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnowGlobeView.swift; sourceTree = "<group>"; };
A774FBD51FB32DD000D08796 /* SnowGlobeSensitivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnowGlobeSensitivity.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -85,6 +87,7 @@
children = (
9FDC70C11A2FBFAB0033F371 /* SnowGlobe.h */,
9FDC70DD1A2FBFDC0033F371 /* SnowGlobeView.swift */,
A774FBD51FB32DD000D08796 /* SnowGlobeSensitivity.swift */,
9FDC70D81A2FBFDC0033F371 /* CMMotionManager+shared.swift */,
9FDC70DB1A2FBFDC0033F371 /* Numbers+Extentios.swift */,
9FDC70E41A2FBFE70033F371 /* Resources */,
Expand Down Expand Up @@ -242,6 +245,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A774FBD61FB32DD000D08796 /* SnowGlobeSensitivity.swift in Sources */,
9FDC70E11A2FBFDC0033F371 /* Numbers+Extentios.swift in Sources */,
9FDC70DE1A2FBFDC0033F371 /* CMMotionManager+shared.swift in Sources */,
9FDC70E31A2FBFDC0033F371 /* SnowGlobeView.swift in Sources */,
Expand Down
36 changes: 36 additions & 0 deletions SnowGlobe/SnowGlobe/SnowGlobeSensitivity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// SnowGlobeSensitivity.swift
// SnowGlobe
//
// Created by Jan Dammshäuser on 08.11.17.
// Copyright © 2017 stringCode. All rights reserved.
//

import Foundation

/// The SnowGlobeSensitivity defines how hard you have to shake the device in order to make it snow.
///
/// - ultraLow: 1.7
/// - low: 2
/// - medium: 3 (default)
/// - high: 4
/// - custom: enter your own sensitivity value. Has to be positive.
public enum SnowGlobeSensitivity {
case ultraLow, low, medium, high
case custom(Double)

fileprivate var value: Double {
switch self {
case .ultraLow: return 1.7
case .low: return 2
case .medium: return 3
case .high: return 4
case let .custom(sensitivity) where sensitivity > 0: return sensitivity
case .custom: return 0
}
}
}

func >=(lhs: Double, rhs: SnowGlobeSensitivity) -> Bool {
return lhs >= rhs.value
}
8 changes: 5 additions & 3 deletions SnowGlobe/SnowGlobe/SnowGlobeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ open class SnowGlobeView: UIView {

open var soundEffectsEnabled: Bool = true

open var sensitivity: SnowGlobeSensitivity = .medium

/// default ligth snow flake image
open class func lightSnowFlakeImage() -> (UIImage?) {
if let image = UIImage(named: "flake") {
Expand Down Expand Up @@ -170,9 +172,9 @@ open class SnowGlobeView: UIView {
}
motionManager.startAccelerometerUpdates(to: queue) { [weak self] accelerometerData, error in
let data = accelerometerData!.acceleration
var magnitude = sqrt( sq(data.x) + sq(data.y) + sq(data.z) )
magnitude = (magnitude < 3.0) ? 0.0 : magnitude
if (magnitude == 0.0 && self?.isAnimating == false) {
let sensitivity = self?.sensitivity ?? .medium
let magnitude = sqrt( sq(data.x) + sq(data.y) + sq(data.z) )
if (magnitude >= sensitivity && self?.isAnimating == false) {
return
}
if let welf = self {
Expand Down